How can I sort an array returned by an activerecord query by a 'created_at' date column? Keep in mind, this is once the query has been executed. Don't tell me to do it in the query because I need this to happen in the view.
Thanks
How can I sort an array returned by an activerecord query by a 'created_at' date column? Keep in mind, this is once the query has been executed. Don't tell me to do it in the query because I need this to happen in the view.
Thanks
Ruby includes support for sorting out of the box.
sorted = @records.sort_by &:created_at
However, this doesn't appear to have much to do with display and probably belongs in the controller.
Just call sort on the collection, passing in the block of code which tells ruby how you want it to sort:
collection.sort { |a,b| a.created_at <=> b.created_at }