0

那是我的路由:

resources :posts do
   resources :comments
  end

我的用户类:

has_many :posts

我的帖子类:

belongs_to :user
has_many :comments

我的评论课

belongs_to :post

在我的用户模型中,我想检索所有评论。对于帖子,它将是:

def all
  Post.where("user_id = ?", id)
end

如何获得所有评论?

4

1 回答 1

1

我的 Rails 超级生锈,但它不会是这样的:

def all
    Comment.joins(:post).where(:posts => {:user_id => id})
end
于 2013-03-24T14:01:15.840 回答