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)
是否也可以获得评论用户?我列出了很多帖子和评论。我不想查询每个评论用户。
谢谢/托比亚斯
I need to query all posts from a specific user and include all comments and the user who belongs to the comment.
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)
Is is possible to also get the comment user? I list a lot of posts and comments. I do not want to query each comment user.
Thx / Tobias
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
在此处阅读更多相关信息
Try
Read more about it here
包含在关系定义语句中怎么样?
How about include at the relation definition statement?