Module 16 Β· Query Performance β Lesson 2 of 4 Β· ~8 min
includes, strict_loading & Friends
.includes β one method, two strategies, Rails picks
.includes(:walker, :dog) doesn't commit to a single SQL shape. Rails looks at the rest of your query and picks one of two strategies for you:
- Preload (separate queries):
SELECT FROM bookings ..., thenSELECT FROM walkers WHERE id IN (...), thenSELECT * FROM dogs WHERE id IN (...). Three total queries, each simple, results stitched together in Ruby. This is whatindexgets, because nothing in the query filters or orders by a column onwalkersordogs.
- Eager load (one JOIN): a single
LEFT OUTER JOINquery. Rails switches to this automatically the moment your.whereor.orderreferences a column on the included association β because a preload's separate queries can't apply a condition against a table they haven't joined yet.
You can force either by name β .preload(:walker, :dog) always does separate queries, .eager_load(:walker, :dog) always does one JOIN β but plain .includes is the right default almost everywhere: let Rails pick.