Rails 3 ways to do eager loading (preloading) in Rails 3 & 4

rubyu2 · 2015年03月18日 · 1774 次阅读



There are 3 ways to do eager loading in Rails:

  • #includes
  • #preload
  • #eager_load

#includes delegates the job to #preload or #eager_load depending on the presence or absence of condition related to one of the preloaded table.

#preload is using separate DB queries to get the data.

#eager_load is using one big query with LEFT JOIN for each eager loaded table.

In Rails 4 you should use #references combined with #includes if you have the additional condition for one of the eager loaded table.

需要 登录 后方可回复, 如果你还没有账号请 注册新账号