【发布时间】:2011-11-22 07:46:33
【问题描述】:
使用这些模型:
class Week
has_many :proofs
end
class Proof
belongs_to :week
end
我想做这样的事情:
Week.where(:proof.count.gt => 0)
只查找有多个证明的周。
似乎有一个答案可以解决这个问题:
Can rails scopes filter on the number of associated classes for a given field
但在本例中,Week 中没有 proof_ids 这样的属性,因为 id 与证明一起存储。这不起作用,例如:
Week.where(:proof_ids.gt => 0)
这个查询怎么可能?概念上很简单,但我不知道如何使用 mongo 或 mongoid 来做到这一点。
同样,我想按证明的数量排序,例如:
Week.desc(:proofs.size)
但这也行不通。
我确实意识到计数器缓存是我的两个具体问题的一个选项,但我也希望能够进行查询。
提前感谢您的帮助。
【问题讨论】:
标签: ruby-on-rails mongoid has-many