Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eager loading in deep level nested association

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.

like image 945
user1787700 Avatar asked May 05 '15 14:05

user1787700


1 Answers

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.

like image 139
cschroed Avatar answered Nov 08 '22 06:11

cschroed