Forum: Ruby on Rails Changing accessor list order

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jason S. (Guest)
on 2006-03-24 02: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'

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?
Jason S. (Guest)
on 2006-03-24 02:44
Ok, I think I already solved my own problem.  Array.reverse_each.  So
many fantastic tricks that I still have yet to learn!
Jason S. (Guest)
on 2006-03-24 02: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
Tom M. (Guest)
on 2006-03-24 03:00
(Received via mailing list)
See also the :order option for has_many relationships.

-- Tom M.
Jason S. (Guest)
on 2006-03-24 06: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.