0

例如,Posthas_many Comments

如何找到所有没有评论的帖子?

4

2 回答 2

3

为了获得更好的性能,请使用 counter_cache 列:

belongs_to :post, :counter_cache => true

api.rubyonrails.org , Railscasts

然后你可以这样做:

Post.where("comments_count = ?", 0)

于 2012-05-24T15:46:40.870 回答
2

您可以使用 not exists ,例如:

Post.where(" not exists (select 'x' from comments where comments.post_id = posts.id)")
于 2012-05-24T15:44:03.117 回答