Including multiple Associations in a query

I hope to explain my problem using the tables below:

university = University.where('' => 'example', 'class.students.age' => '21').includes(:class, :student)




As you can see the University table references the Class table. The Class table references the Student table.

I want to get the universities that have a class called example and the students are aged 21. I know it weird but it shows my problem :D

I can't get my query to get hold of the Student. I always get the error "Association named 'students' was not found"

It works fine if I do the following without using the Student table

university = University.where('' => 'example').includes(:class)


First of all, be advised that it's not a good idea to name a model "Class" since that's a reserved word in Ruby (for classes), which may cause trouble.

Aside from that: In the University model, you need to specify a relation to the Student model like this:

has_many :students, :through => :class

Then you can get what you want with a relation like this:

university = University.where('' => 'example', 'students.age' => 21).includes(:class, :students)

Need Your Help

Passing by Reference in JavaScript(Call Method)

javascript prototype pass-by-reference

my problem here that i don't understand how does the method called "objectchanger" works

How do I get a value in the `onchange` event of a Blockly custom block?

javascript blockly

I am making a custom block for Blockly and need to validate the inputs. In the onchange event, I want to warn the user if they are entering an invalid value for the input.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.