Ruby代码美化,在多行上分割长指令

Eri*_*rch 41 ruby code-formatting

我们如何编写以下语句以提高可读性?

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)
Run Code Online (Sandbox Code Playgroud)

以下内容无法编译

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])
Run Code Online (Sandbox Code Playgroud)

Eri*_*rch 56

也有可能做到

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)
Run Code Online (Sandbox Code Playgroud)


Geo*_*Geo 50

像这样做:

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)
Run Code Online (Sandbox Code Playgroud)


Mch*_*chl 14

它应该在1.9中编译.在以前的版本中它确实无效.