2011-02-01 39 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 

然后你就可以加载您的帖子。

相关问题