2012-01-16 111 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