Rails 3获取嵌套记录

时间:2011-09-01 20:42:28

标签: ruby-on-rails ruby-on-rails-3 activerecord

我的设置:Rails 3.0.9,Ruby 1.9.2

型号:

class User
 has_many :posts
 has_many :photos

class Post
 belongs_to :user
 has_many :comments

class Photo
 belongs_to :user
 has_many :comments

class Comment
 belongs_to :post
 belongs_to :photo

User级别,我想抓住user.postsuser.photos附带的所有评论,是否可能,如果是,那么语法是什么?

1 个答案:

答案 0 :(得分:4)

user.posts.map(&:comments)user.photos.map(&:comments)