Rails按关联数据排序

时间:2012-01-16 02:25:57

标签: ruby-on-rails activerecord associations

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

1 个答案:

答案 0 :(得分:2)

如果您使用rails 3.x

,这应该可以使用
school.classrooms.includes(:teacher).order("teachers.name")