【发布时间】:2025-12-25 21:40:15
【问题描述】:
class Post
has_many :commments
end
class Comment
belongs_to :post
end
我希望显示按post 创建日期排序的posts 列表(submitted_at)。我还希望一些post xyz 出现在顶部,如果它发布了一些新评论但尚未由版主审核。我们将通过 cmets 级别的 boolean 属性/字段来确定这一点(moderated = 1/0)
我试过了
Posts.join(:comments)
.distinct
.order("submitted_at DESC, comments.moderated")
但这不包括没有comments 且结果未按预期排序的帖子。我确信我们可以在 ruby 级别做到这一点,但正在寻找一种使用 AR 来做到这一点的方法。
【问题讨论】:
-
用“include”代替“join”怎么样
标签: ruby-on-rails ruby-on-rails-4 activerecord