<%= render :partial => 'event', :collection => @events.sort_by(&:event_at)%>
This code shows a collection ordered as ASC, but i want to order this collection as DESC.
How can I achieve this?
<%= render :partial => 'event', :collection => @events.sort_by(&:event_at)%>
This code shows a collection ordered as ASC, but i want to order this collection as DESC.
How can I achieve this?
This should do it =
@events.sort {|a,b| b <=> a}
You can also see the sort documentation on Enumerables.
Even better, you can set a scope to sort your event and use it in your render.
In your Event model:
scope :desc, order("events.event_at DESC")
If you use Rails3, in your view you can simply do:
<%= render @events.desc %>
You can just reverse the sorted collection:
<%= render :partial => 'event', :collection => @events.sort_by(&:event_at).reverse %>
but as Yannis says you are better off sorting as you fetch things from the database ideally.