6

In Ruby on Rails I have School which has many children. Children which has many activities. And Activity which has one activity_types. I need help nesting includes. In my Children Controller. I have this... which works.

s = School.find(params[:school_id])
@school = s
@children = s.children.includes(:activities).all

But I want to also get the :activity_type from the activities from the children. I tried this

s = School.find(params[:school_id])
@school = s
@children = s.children.includes(:activities => :activity_types).all

But that did not work

Johnston
  • 20,196
  • 18
  • 72
  • 121

1 Answers1

9

Don't pluralize activity_type.

s.children.includes(:activities => :activity_type).all

James
  • 4,797
  • 25
  • 29
  • Wow thank you. Why does that work? What is the logic behind that? – Johnston Feb 25 '12 at 20:57
  • I'm assuming you made a typo when you wrote activity has one activity_types (plural) because you later wrote that you wanted to get the activity_type from an activity. `children.includes(:activities => :activity_type)` means include all activities for each children and include each activity_type for each activity. If you use the plural (activity_types) then that assumes that activities have many activity_types and not just one. – James Feb 25 '12 at 21:03