2012-01-16 108 views
0

是否可以按教师姓名排列school.classrooms的结果?我想直接在关联中这样做,而不是单独调用。Rails关联数据排序

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 

回答

2

这应该如果你正在使用的轨道3.X

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

+1注工作 - 这个答案没有这样做“在联想” ......但我认为这是一件好事!因为实际上这样做是错误的。这样做*使用*这个例子中显示的关联是正确的形式。 – 2012-01-16 03:37:04

+0

为什么在关联中这样做是错误的? – Dave 2012-01-16 14:04:56