3 ms·
That is not the same query at all. You need to do a left join and a group by for it to become the same query. Wihout a left join posts without comments wont sho
by jeltz 10y ago
That is not the same query at all. You need to do a left join and a group by for it to become the same query. Wihout a left join posts without comments wont show up in the result.
- fbonetti 10y agoYou're totally right. My example is an inner join. I will admit that doing outer joins in ActiveRecord is quite painful. Here's a more realistic example, which is not nearly as pretty: Post.select("posts.*, COUNT(comments.*) AS comments_count").joins("LEFT JOIN comments ON comments.post_id = posts.id").group(:comments)