Bob
Bob

Reputation: 8494

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?

Upvotes: 2

Views: 144

Answers (1)

cpjolicoeur
cpjolicoeur

Reputation: 13106

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

Upvotes: 4

Related Questions