0

我有一些这样的模型:

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;
4

2 回答 2

1

不应该加入

Student.rich_students.joins(:teacher)

?

于 2013-03-06T21:38:06.027 回答
1

我认为您的学生“belongs_to”声明中应该有错误

class Student < ActiveRecord::Base
  belongs_to :teacher
  scope :rich_students, joins(:teachers).order('students.money DESC')
end

“:老师”而不是“:老师”

希望这可能是问题的原因...

干杯

于 2013-03-07T09:16:37.437 回答