2011-09-09 11 views
35

Jak możemy napisać następujące oświadczenie, aby poprawić czytelność?Upiększanie kodu ruby, dzielenie długich instrukcji na wiele linii

Promotion.joins(:category).where(["lft>=? and rgt<=?", c.lft, c.rgt]).joins(:shops).where(:promotions_per_shops => { :shop_id => shops_id }).count('id', :distinct => true) 

Następujące nie skompilować

Promotion.joins(:category) 
     .where(["lft>=? and rgt<=?", c.lft, c.rgt]) 
     .joins(:shops) 
     .where(:promotions_per_shops => { :shop_id => shops_id }) 
     .count('id', :distinct => true) 

syntax error, unexpected '.', expecting kEND 
        .where(["lft>=? and rgt<=?", c.lft, c.rgt]) 

Odpowiedz

44

Czy to tak:

Promotion.joins(:category). 
     where(["lft>=? and rgt<=?", c.lft, c.rgt]). 
     joins(:shops). 
     where(:promotions_per_shops => { :shop_id => shops_id }). 
     count('id', :distinct => true) 
14

Należy skompilować w 1,9. W poprzednich wersjach było ono faktycznie nieważne.

48

także możliwe do zrobienia

Promotion.joins(:category) \ 
     .where(["lft>=? and rgt<=?", c.lft, c.rgt]) \ 
     .joins(:shops) \ 
     .where(:promotions_per_shops => { :shop_id => shops_id }) \ 
     .count('id', :distinct => true) 
+0

oznaczenie '\', co jest bardzo pomocne! Dzięki –

Powiązane problemy