[英]Select all posts where comments.count >= 10
One Post有很多評論。 我想選擇comments.count大於5的所有帖子。
像這樣:Post.all.joins(:comments).having(“count> = 5”)
如果您在posts表中添加了comments_count
列,請添加以下內容:
class Comment < ActiveRecord::Base
belongs_to :post, :counter_cache => true
..
end
然后,當您保存帖子時,它將更新計數器。 在這一點上,查詢很簡單:
Post.where("comments_count >= ?", 5).all
或者,您可以使用純SQL代碼。 但男人,只是去櫃台
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.