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? -- Posted via http://www.ruby-forum.com/.
Ok, I think I already solved my own problem. Array.reverse_each. So many fantastic tricks that I still have yet to learn! -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
See also the :order option for has_many relationships. -- -- Tom Mornini On Mar 23, 2006, at 4:40 PM, Jason Sallis wrote:> 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails