2011-02-01 40 views
0

職位表排序由孩子的一個值一對多的關係

int id 

意見表

int id 
int post_id 
datetime created_at 

我需要按這篇文章的評論created_at帖子。我試過類似的東西,但我不能選擇明確的後ids.Any幫助將不勝感激。

Post.all(:joins => :comments, :order => 'comments.created_at')  

我想看最近評論過的帖子。

回答

0

您傳遞的條件無效。 你可以完成你想要做的最簡單的方法是在你的帖子表中添加一列 - 比如last_commented_at。 在你Comment模型通過調用

Post.order("last_commented_at DESC") 

顯示第一的職位,最近已經評論添加回調

class Comment < ActiveRecord::Base 
    belongs_to :post 

    after_save :update_posts_last_commented_attribute 

    def update_posts_last_commented_attribute 
    post.update_attribute(:last_commented_at, updated_at) 
    end 
end 

然後你就可以加載您的帖子。

相關問題