我有一些这样的模型:
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;