What is the \"rails way\" to efficiently grab all rows of a parent table along with a count of the number of children each row has?
I don\'t want to use counte
counte
For Rails 3, you'd be looking at something like this:
Article.select( "articles.*, count(comments.id) AS comments_count" ) .joins( "LEFT OUTER JOIN comments ON comments.article_id = articles.id" ) .group( "articles.id" )
Thanks to Gdeglin for the Rails 2 version.