How do I order a has_many through association in Ruby on Rails?
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 :tasks, :through => :assignments
#assignment
belongs_to :task
belongs_to :user
#task
has_many :assignments
has_many :users, :through => :assignments
I would like to get a task then navigation to its assigned users, and sort the user list alphabetically.
I keep thinking that I should be able to add the :order
clause to has_many :users, :through => :assignments
like this:
#task.rb
has_many :assignments
has_many :users, :through => :assignments, :order => 'last_name, first_name'
however this does not work.
How can I sort users by last_name
when given a task?
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
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.