是否可以按老师的名字订购school.classrooms的结果?我想直接在关联中执行此操作,而不是单独调用。
class School < ActiveRecord::Base
has_many :classrooms
end
class Classroom < ActiveRecord::Base
belongs_to :school
belongs_to :teacher
end
class Teacher < ActiveRecord::Base
has_one :classroom
end
答案 0 :(得分:2)
如果您使用rails 3.x
,这应该可以使用school.classrooms.includes(:teacher).order("teachers.name")