我有一些这样的模型:
class Student < ActiveRecord::Base
belongs_to :Teacher
scope :rich_students, joins(:teachers).order('students.money DESC')
end
然后是班主任
class Teacher < ActiveRecord::Base
has_many :students
belongs_to :Organization
end
然后:
class Organization < ActiveRecord::Base
has_many :teachers
end
现在我写一个这样的查询:
Student.rich_students.joins(:teachers).where("teachers.organization_id = ?", params[:id]).limit(5)
但这不起作用。它给了我错误:
Association named 'teachers' was not found;
答案 0 :(得分:1)
不应该加入
Student.rich_students.joins(:教师)
答案 1 :(得分:1)
我认为您的学生“belongs_to”声明中存在错误,应该是
class Student < ActiveRecord::Base
belongs_to :teacher
scope :rich_students, joins(:teachers).order('students.money DESC')
end
“:老师”而不是“:老师”
希望它可能是事情的原因......
干杯