Even if you already know what is the difference between
eager_load, maybe is not that clear when is better for you to use one vs the other.
Well, as you may know, with Rails/ActiveRecord you can preload associations in 2 different ways:
When you use
includes Rails will decide which strategy to use for you. I think that this is a good default. Use
includes and let rails decide for you.
But, to be honest, that is not what I do…
I (at least for now) prefer to use
preload explicitly by default, because that is also the default for
includes, and I think that is easier to know
what is going to happen just by watching the code.
eager_load just when I already know that is significantly faster than the
I would say, “Don’t worry to much”, and just pick one of this two strategies.
Practice will help you understand this concepts better. I have prepared some examples/excercises to help you understand how you can use this methods. Give it try!
Sign up to download my free ebook, where I will show you how to deal with n+1 problems like a pro, by helping you...
includes, etc...) to work with associations.