How do I order a has_many through association in Ruby on Rails?

前端 未结 8 1277
北恋
北恋 2020-12-15 02:45

Given the following AR models, I would like to sort users alphabetically by last name when given a handle to a task:

#user
has_many :assignments
has_many :ta         


        
8条回答
  •  Happy的楠姐
    2020-12-15 02:53

    Rails 3.x version:

    has_many :users, :through => :assignments, :order => 'users.last_name, users.first_name'
    

    UPDATE: This only works in Rails 3.x (maybe before that too). For 4+, see other answers.

提交回复
热议问题