1
User
has_many Post
。通過最大的評論來訂購文章
Post
has_many Comment
。
如何查詢用戶的帖子,按每個帖子的評論數量排序?
儘可能多的ActiveRecord-esque答案將不勝感激。
User
has_many Post
。通過最大的評論來訂購文章
Post
has_many Comment
。
如何查詢用戶的帖子,按每個帖子的評論數量排序?
儘可能多的ActiveRecord-esque答案將不勝感激。
商店的評論數在Comment
模型職位爲counter_cache
:
belongs_to :post, counter_cache: true
在
posts
表
然後,有一個comments_count
字段是一個整數。
從那裏,這很容易:
Post.order("comments_count DESC")