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

前端 未结 8 1297
北恋
北恋 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条回答
  •  忘掉有多难
    2020-12-15 03:04

    Since condition arguments are deprecated in Rails 4, one should use scope blocks:

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

提交回复
热议问题