Forum: Ruby on Rails Changing accessor list order

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E68d4935996d7f13cdb33ac6dc50acb7?d=identicon&s=25 Jason Sallis (jsallis)
on 2006-03-24 01:40
Ok, I'm pretty new to Rails and I'm building a bug tracker as my first
real project. I have a situation where a bug can have multiple comments.
Pretty simple.  When I show the comments on the screen, I want to make
sure that they're displayed in reverse order by date created.  I have
used the following to change the order of @bug.comments:

class Bug < ActiveRecord::Base
  belongs_to :priority
  has_many :comments
  validates_presence_of :description, :priority

  def comments
    Comment.find :all, :order => 'created_at DESC'
  end
end

It works like a charm.  But, is there a better way to do this?  Should I
be returning it as-is and reversing it in the view instead?
E68d4935996d7f13cdb33ac6dc50acb7?d=identicon&s=25 Jason Sallis (jsallis)
on 2006-03-24 01:44
Ok, I think I already solved my own problem.  Array.reverse_each.  So
many fantastic tricks that I still have yet to learn!
E68d4935996d7f13cdb33ac6dc50acb7?d=identicon&s=25 Jason Sallis (jsallis)
on 2006-03-24 01:44
Oops, too vague.  To clarify, I removed the accessor override and just
performed an Array.reverse_each in the view to list comments in reverse
order.
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-03-24 02:00
(Received via mailing list)
See also the :order option for has_many relationships.

--
-- Tom Mornini
E68d4935996d7f13cdb33ac6dc50acb7?d=identicon&s=25 Jason Sallis (jsallis)
on 2006-03-24 05:57
That's even better.  I didn't realize there were so many options for
has_many.  Thx Tom.
This topic is locked and can not be replied to.