【问题标题】:Ruby code beautification, split long instructions on multiple linesRuby代码美化,多行拆分长指令
【发布时间】:2011-11-13 16:02:23
【问题描述】:

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

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

【问题讨论】:

    标签: ruby code-formatting


    【解决方案1】:

    这样做:

    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)
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        也可以这样做

        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)
        

        【讨论】:

        • “\”这个东西很有帮助!谢谢
        猜你喜欢
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        • 2020-05-14
        • 2010-09-06
        • 1970-01-01
        • 2023-03-12
        • 2015-08-09
        • 2012-03-28
        相关资源
        最近更新 更多