Ruby on rails 在查询中包含多个关联

Ruby on rails 在查询中包含多个关联,ruby-on-rails,ruby,Ruby On Rails,Ruby,我希望用下表来解释我的问题: university = University.where('class.name' => 'example', 'class.students.age' => '21').includes(:class, :student) University id class.id name Class: id student.id name MaximumStudents Student: id name age 正如您可以看到的那样,大学表引用了类表。

我希望用下表来解释我的问题:

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

University
id
class.id
name

Class:
id
student.id
name
MaximumStudents

Student:
id
name
age
正如您可以看到的那样,
大学
表引用了
表。
Class
表引用了
Student

我想找那些开设了名为
example
的课程,学生年龄
21岁的大学。我知道这很奇怪,但它显示了我的问题:D

我无法获取我的查询以获取
学生
。我总是收到错误“找不到名为“学生”的关联”

如果我在不使用
Student
表的情况下执行以下操作,则效果良好

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

首先,请注意,将模型命名为“Class”不是一个好主意,因为在Ruby中这是一个保留字(用于类),这可能会引起麻烦

除此之外:在
大学
模型中,您需要指定与
学生
模型的关系,如下所示:

has_many :students, :through => :class
university = University.where('class.name' => 'example', 'students.age' => 21).includes(:class, :students)
然后你可以通过这样的关系得到你想要的:

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

谢谢成功了。上述内容实际上不是我的申请。我不该上课。