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

rubyu2 · March 18, 2015 · 1855 hits

搬运工:http://blog.arkency.com/2013/12/rails4-preloading/

Summary

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.

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.