user1899082
user1899082

Reputation:

Writing nested associations

From an Organization table two other tables are hanging with belongs_to and has_many associations, they both have that organization_id key.

enter image description here

So for eager loading I wrote this:

@organization = Organization.includes(:population_summaries, :key_performance_inds).find(params[:id]) 

This should work.

But now I have a third table to include in there. key_performance_interval which has that kpi_id field that is used as foreign key to point to key_performance_int table. I can't get that part to write and add to my eager loading section. How should I add that one?

Upvotes: 0

Views: 74

Answers (1)

Shane Andrade
Shane Andrade

Reputation: 2675

To included nested associations, you write it as a hash:

@organization = Organization.includes([:population_summaries, key_performance_inds:  :key_performance_interval]).find(params[:id])

You can read about it on this rails guide under section 12.1.2

Upvotes: 1

Related Questions