1
One Post有很多意见。我想选择的所有帖子,其中comments.count大于5选择所有帖子where comments.count> = 10
事情是这样的:Post.all.joins(:评论).having(“数> = 5”)
One Post有很多意见。我想选择的所有帖子,其中comments.count大于5选择所有帖子where comments.count> = 10
事情是这样的:Post.all.joins(:评论).having(“数> = 5”)
如果添加一个comments_count
列到你的职位表,补充一点:
class Comment < ActiveRecord::Base
belongs_to :post, :counter_cache => true
..
end
然后当你保存后,就会更新计数器。在这一点上,查询非常简单:
Post.where("comments_count >= ?", 5).all
或者,您可以放入纯SQL代码。但人,只是去柜台
相关:Rails query that sorts based on the count of a nested model?
您是否在寻找RoR的代码,或者是你想编写一个SQL查询? – leonsas 2012-07-18 16:24:56
RoR最好:) – Hendrik 2012-07-18 16:31:16
不能帮忙,对不起! – leonsas 2012-07-18 16:36:44