June 2024
Intermediate to advanced
456 pages
11h 34m
English
Besides .preload, .includes is another method for eager loading. This method produces an equivalent SQL query to .preload.
Try running the following example using .includes and compare the generated query to the one from earlier:
| | Vehicle.includes(:vehicle_reservations).each do |vehicle| |
| | vehicle.vehicle_reservations.size |
| | end; nil |
With .preload, the WHERE clause conditions for the second table being joined cannot be modified. The SQL query used an IN clause for the top-level query with a list of primary key values from the second table.
If .includes generates the same SQL query, then why does this method exist?
Another trick that .includes has is that it can pick ...
Read now
Unlock full access