zulhfreelancer
12/12/2016 - 10:23 AM

[Rails performance] - GROUP and JOIN with association's count

[Rails performance] - GROUP and JOIN with association's count

posts = Post.joins(:comments).group("posts.id").select("posts.*, count(comments.id) as comment_count")

posts.first.comment_count
=> 7