I have models having nested associations. I want to load all nested records from just one query.
hotel has_many rooms
rooms has_many room_variants
room_variant has_many seasonal_rates
I search for hotel with hotel name, and want to load nested model data.
For one level nesting we can do Hotel.search('test').includes(:rooms)
. I am not finding a way to load deep level nested association model entries.
You can do:
Hotel.search('test').includes(rooms: { room_variants: :seasonal_rates })
See the "Nested Association Hash" section of Eager Loading Associations in the RailsGuides.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With