Rails 3 getting nested records
My setup: Rails 3.0.9, Ruby 1.9.2
Models:
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
At the User
level, I would like to grab all the comments attached to user.posts
and user.photos
, is that possible and if so, what's the syntax?
user.posts.map(&:comments)
or user.photos.map(&:comments)
精彩评论