2011-09-09 12 views
35

Come possiamo scrivere la seguente dichiarazione per migliorare la leggibilità?Abbellimento codice rubino, istruzioni lunghe divise su più righe

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) 

Quanto segue non compila

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

risposta

44

fare in questo modo:

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

Si dovrebbe compilare in 1.9. Nelle versioni precedenti era davvero invalido.

48

anche possibile fare

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

La cosa '\' è molto utile! Grazie –

Problemi correlati