Ruby code beautification, split long instructions on multiple lines

前端 未结 3 1571
悲&欢浪女
悲&欢浪女 2021-02-05 00:22

How can we write the following statement to improve readability?

Promotion.joins(:category).where([\"lft>=? and rgt<=?\", c.lft, c.rgt]).joins(:shops).wher         


        
相关标签:
3条回答
  • 2021-02-05 00:30

    Also possible to do

    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 讨论(0)
  • 2021-02-05 00:36

    Do it like this:

    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 讨论(0)
  • 2021-02-05 00:39

    It should compile in 1.9. In previous versions it was invalid indeed.

    0 讨论(0)
提交回复
热议问题