class Post
has_many :commments
end
class Comment
belongs_to :post
end
通過屬性順序的ActiveRecord對象我希望顯示的post
創建(submitted_at
)的日期排序的posts
列表。我還想要一些post
xyz出現在頂部,如果它有一些新的評論發佈,但尚未由主持人審查。我們將在註釋水平boolean
屬性/場(主持= 1/0)Rails的:下的has_many關係
確定這個我試過
Posts.join(:comments)
.distinct
.order("submitted_at DESC, comments.moderated")
,但這不包括那些沒有comments
和預期結果進行排序的職位。我相信我們可以在ruby層面上做到這一點,但是尋找一種使用AR來做到這一點的方法。
如何使用「包括」,而不是「加入」 – chaitanya
可能是這將有助於http://stackoverflow.com/questions/3245201/left-outer-joins-in-rails-3 – chaitanya