【问题标题】:nested joins statement in rails appRails 应用程序中的嵌套连接语句
【发布时间】:2014-12-02 08:42:06
【问题描述】:

在我的 Rails 应用程序中,我有四个模型(比如 A、B、C、D、E 和 F)

  • A 属于 B
  • B 有很多 Cs
  • C 属于 D
  • D 属于 E 和 F

我正在尝试如下构建查询

scope = A.joins(:b, { b: [:cs, {cs: [:d, {d: [:e,:f] } ] } ] })

但它不起作用。错误信息是schema cs (plural of c) does not exist

我正在使用Postgresql

【问题讨论】:

  • 别忘了:joins(:c) 然后joins(:d)
  • 你能显示结果查询吗?

标签: ruby-on-rails postgresql join


【解决方案1】:

好吧,您似乎在 C 模型中缺少 B 引用 (b_id)。 C 需要知道它属于哪个 B。之后,您可以将查询简化为:

A.joins(b: {cs: {d: [:e, :f]}})

【讨论】:

  • 我有参考资料(没有它,应用程序将无法正常工作)。但是感谢更短的表格,尽管我仍然有相同的错误消息
  • 这很奇怪。它对我有用gist.github.com/anonymous/4a91183837d10ecbb7bf
  • 错误出现在 where 子句上。 scope.where("lower(c.d.description) like ?", "%#{params[:description]}%")
  • 试试scope.where("lower(ds.description) like ?", "%#{params[:description]}%")
  • 工作。非常感谢!
猜你喜欢
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 2012-05-07
相关资源
最近更新 更多