Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Includes in Ruby on Rails 3

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

like image 880
Johnston Avatar asked Feb 25 '12 15:02

Johnston


People also ask

What is Ruby on rails and how does it work?

Ruby on Rails is a web application framework written in Ruby that offers developers an opinionated approach to application development. Working with Rails gives developers: Conventions for handling things like routing, stateful data, and asset management.

Why add related models to a Rails application?

Adding related models means establishing meaningful relationships between them, which then affect how information gets relayed through your application’s controllers, and how it is captured and presented back to users through views. In this tutorial, you will build on an existing Rails application that offers users facts about sharks.

What is an example of a nested attribute?

The simplest example of Nested Attributes is with a one-to-one association. To add Nested Attributes support to the product model all you need to do is add the following line: What does this do, exactly?

What can you do with a Rails application?

With your Rails application in place, you can now work on things like styling and developing other front-end components. If you would like to learn more about routing and nested resources, the Rails documentation is a great place to start.


1 Answers

Don't pluralize activity_type.

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

like image 196
James Avatar answered Sep 28 '22 07:09

James