Reputation: 1111
I have Posts and Comments tables. They have belongs_to and has_many relation. Everything works great.
What I need to do is writing the SQL to pull posts that have comments. How can I do that in the controller?
I need some sort of Join I guess. Right?
Thank you
Upvotes: 1
Views: 24
Reputation: 33420
Post.joins(:comments)
by itself will give you all those post that do have comments related.
The INNER JOIN
does that work. When you use an inner join between two tables it returns a new set of data with all of the instances of the join where the condition was met, the rows are ignored otherwise.
Upvotes: 1