113

This seems like a really simple question but I haven't seen it answered anywhere.

In rails if you have:

class Article < ActiveRecord::Base 
  has_many :comments 
end 
class Comments < ActiveRecord::Base 
  belongs_to :article 
end

Why can't you order the comments with something like this:

@article.comments(:order=>"created_at DESC")

Named scope works if you need to reference it a lot and even people do stuff like this:

@article.comments.sort { |x,y| x.created_at <=> y.created_at }

But something tells me it should be simpler. What am I missing?

1
  • Be careful, you are using an unexpected method: @article.comments(reload = false) is for forcing a cache-miss (to force the reload of a relation). If you provide a hash, it's the same as @article.comments(true). Don't forget to use .all(:order => '...'). Broke my leg a few times already. Apr 12, 2009 at 12:22

5 Answers 5

159

You can specify the sort order for the bare collection with an option on has_many itself:

class Article < ActiveRecord::Base 
  has_many :comments, :order => 'created_at DESC'
end 
class Comment < ActiveRecord::Base 
  belongs_to :article 
end

Or, if you want a simple, non-database method of sorting, use sort_by:

article.comments.sort_by &:created_at

Collecting this with the ActiveRecord-added methods of ordering:

article.comments.find(:all, :order => 'created_at DESC')
article.comments.all(:order => 'created_at DESC')

Your mileage may vary: the performance characteristics of the above solutions will change wildly depending on how you're fetching data in the first place and which Ruby you're using to run your app.

3
61

As of Rails 4, you would do:

class Article < ActiveRecord::Base 
  has_many :comments, -> { order(created_at: :desc) }
end 
class Comment < ActiveRecord::Base 
  belongs_to :article 
end

For a has_many :through relationship the argument order matters (it has to be second):

class Article
  has_many :comments, -> { order('postables.sort' :desc) }, 
           :through => :postable
end

If you will always want to access comments in the same order no matter the context you could also do this via default_scope within Comment like:

class Comment < ActiveRecord::Base 
  belongs_to :article 
  default_scope { order(created_at: :desc) }
end

However this can be problematic for the reasons discussed in this question.

Before Rails 4 you could specify order as a key on the relationship, like:

class Article < ActiveRecord::Base 
  has_many :comments, :order => 'created_at DESC'
end 

As Jim mentioned you can also use sort_by after you have fetched results although in any result sets of size this will be significantly slower (and use a lot more memory) than doing your ordering through SQL/ActiveRecord.

If you are doing something where adding a default order is cumbersome for some reason or you want to override your default in certain cases, it is trivial to specify it in the fetching action itself:

sorted = article.comments.order('created_at').all
5
  • 1
    Where can I specify it in the fetching action itself? Do I override a method in the model?
    – Wit
    Jun 21, 2017 at 8:05
  • @Wit - you can add .order() to the method chain, like in the last example. Is this what you are asking? Oct 3, 2017 at 18:27
  • I am sorry. I can't remember what I was trying to achieve.
    – Wit
    Dec 16, 2017 at 9:13
  • 1
    The has_many, through polymorphic example is super helpful here!
    – Vijay
    Aug 21, 2020 at 2:18
  • 1
    Warning: if you do the lambda in rails 6, yo lose the implicit inverse_of, so if you try to save the record with a new record in a has_many, it will not autosave the child first like it will if you don't have the lambda. Apr 1, 2021 at 22:22
7

If you are using Rails 2.3 and want to use the same default ordering for all collections of this object you can use default_scope to order your collection.

class Student < ActiveRecord::Base
  belongs_to :class

  default_scope :order => 'name'

end

Then if you call

@students = @class.students

They will be ordered as per your default_scope. TBH in a very general sense ordering is the only really good use of default scopes.

1
6

You can use ActiveRecord's find method to get your objects and sort them too.

  @article.comments.find(:all, :order => "created_at DESC")

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

1
  • 3
    Or the shorthand method @article.comments.all(:order => 'created_at DESC')
    – erik
    Apr 10, 2009 at 22:07
3

And if you need to pass some additional arguments like dependent: :destroy or whatever, you should append the ones after a lambda, like this:

class Article < ActiveRecord::Base 
  has_many :comments, -> { order(created_at: :desc) }, dependent: :destroy
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.