[英]Rails order by associated data
是否可以按教师的名字订购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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.