How do I get Rails to eager load counts?

后端 未结 4 1778
悲&欢浪女
悲&欢浪女 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:26

    I have set up a small gem that adds an includes_count method to ActiveRecord, that uses a SELECT COUNT to fetch the number of records in an association, without resorting to a JOIN which might be expensive (depending on the case).

    See https://github.com/manastech/includes-count

    Hope it helps!

提交回复
热议问题