2011-09-09 5 views
35

可読性を高めるために次の文を書くにはどうすればよいですか?Rubyコードの美化、複数行の長い命令の分割

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) 

次は

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]) 

答えて

44

はこのようにそれを実行しコンパイルされません:それは1.9でコンパイルする必要があり

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

。以前のバージョンでは、実際には無効でした。また

48

可能

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

を行うには '\' 事は非常に便利です!ありがとう –

関連する問題