0

I found an answer, but it's very outdated (2009).

I would like to know how to sort the collection on a has_many relationship, preferably in the model.

Something like this:

class Article < ActiveRecord::Base 
  has_many :comments, :order => 'created_at DESC'
end 
Community
  • 1
  • 1
Joan Pujol
  • 7
  • 1
  • 2

2 Answers2

3

Have you tried:

class Article < ActiveRecord::Base 
  has_many :comments, -> { order "created_at DESC" } 
end 
Vu Minh Tan
  • 526
  • 4
  • 6
0

There are many syntax changes when you migrated to Rails 4 version. Check this:

http://edgeguides.rubyonrails.org/association_basics.html#scopes-for-has-many

raj_on_rails
  • 144
  • 1
  • 11