Reputation: 5971
I have this db schema :
|-- comment
|-- Post -|-- comment
|
| |-- comment
User --|-- Post -|-- comment
|
| |-- comment
|-- Post -|-- comment
What is the best way to retrieve all comments belonging to one user ?
Thanks
Upvotes: 0
Views: 60
Reputation: 84140
You can us a has_many :through association.
class User
has_many :posts
has_many :comments, :through => :posts
end
class Post
has_many :comments
belongs_to :user
end
class Comment
belongs_to :post
end
With that structure you can simply do:
user.comments
Upvotes: 4