Rails按关联数据排序

是否可以按教师的名字订购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 

如果您使用rails 3.x,这应该可以工作

 school.classrooms.includes(:teacher).order("teachers.name")