我需要查询来自特定用户的所有帖子,并包含所有评论和属于评论的用户。Rails包含嵌套关系
class User < ...
has_many :posts
has_many :comments
end
class Post < ...
belongs_to :user
has_many :comments
end
class Comment < ...
belongs_to :user
belongs_to :post
end
@posts = current_user.posts.include(:comments)
也可以得到评论用户吗?我列出了很多帖子和评论。我不想查询每个评论用户。
THX /托比亚斯
是否可以限制在此查询中的评论。我只想要包含5条最新评论。如果我循环shout.comments.limit(5)一个新的查询运行 – sandelius 2011-12-18 21:33:34
在我的答案:'后has_many:评论,:包括=> [:用户],:限制=> 5' – clyfe 2011-12-18 22:42:20
是的,但我也不想在一个页面上获取所有评论,甚至分页? – sandelius 2011-12-19 06:13:01