Rails 3 - 从相关模型中获取和排序模型的对象

时间:2012-04-15 14:48:04

标签: ruby-on-rails ruby ruby-on-rails-3.2

我有两个模型,教师教师领袖

#  id        :integer
#  last_name :string(255)
class Teacher < ActiveRecord::Base 
  has_one :teacher_leader
end

#  teacher_id :integer
class TeacherLeader < ActiveRecord::Base  
  belongs_to :teacher
end

我想让所有老师领导的老师通过last_name对他们进行排序。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

Teacher.joins(:teacher_leader).uniq.order(:last_name)