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

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

推荐答案

这样做:

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)

Ruby相关问答推荐

使用 RSpec 测试嵌套哈希时随机排序数组的匹配

获取用于传递参数的变量名称

用函数组合枚举器的Ruby方法

有没有办法向用户返回 nil 消息但不中断 ruby​​ 中的应用程序?

Rubymine - 启用行号

如何使用 RSpec 忽略或跳过测试方法?

在 Go (golang) 中编写一个 Ruby 扩展

Ruby 中的 class() 与 type()

如何使用 if..else 块的结果分配变量?

如何在 linux (ubuntu) 上更新 ruby​​?

在文件中搜索字符串的最佳方法是什么?

判断 Ruby HTTP 响应是否成功

try 学习/理解 Ruby 的 setter 和 getter 方法

Rails - create and create!

如何在 ruby​​ 中解压缩 Gzip 字符串?

找不到Bundle 命令 mac

如何使用 Ruby 运行命令五次?

为什么在 ruby​​ 中动态创建大量符号不是一个好主意(对于 2.2 之前的版本)?

如何从字符串创建 Ruby 日期对象?

如何在 Mac OS Sierra 10.12 上安装 Nokogiri