Changing accessor list order

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?

Ok, I think I already solved my own problem. Array.reverse_each. So
many fantastic tricks that I still have yet to learn!

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

See also the :order option for has_many relationships.

– Tom M.

That’s even better. I didn’t realize there were so many options for
has_many. Thx Tom.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs