Even if you already know what is the difference between includes
, preload
and 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:
eager_load
).preload
)And when you use includes
Rails will decide which strategy to use for you. I think that this is a good default.
But sometimes you already know (by using a performance measuring tool) that one strategy is significantly slower than the other…
That’s a good time to use preload
or eager_load
directly, to force explicitly use the strategy that you prefer.
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!
Examples to learn the difference between preload, includes or eager_load