Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I don't know about active record but with Django it's simply:

  Post.objects.all().annotate(Count('comments'))
Which produces (simplified, Django would actually explicitly select each column):

  SELECT posts.*, COUNT(comments.id) AS comments_count
  FROM posts
  LEFT OUTER JOIN comments ON (posts.id = comments.post_id) GROUP BY posts.id
Nice, easy and without 1+N queries.


That is actually very nice and elegant!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: