How do I get Rails to eager load counts?

后端 未结 4 1776
悲&欢浪女
悲&欢浪女 2020-12-14 20:30

This is related to a question a year and change ago.

I put up an example of the question that should work out of the box, provided you have sqlite3 available: https:

4条回答
  •  一向
    一向 (楼主)
    2020-12-14 21:25

    An alternative approach to the one of Zubin:

    Post.select('posts.*, count(comments.id) `comments_count`').joins(:comments).group('posts.id')
    

提交回复
热议问题