Reordering your Objects
06/01/25
I thought I’d share something that I learnt on the rubylist today to do with reordering your results after a query to your database.
1 2 3 4 5 6 7 |
#controller: @categories = @site.categories #view: <% for category in @categories.sort { |a,b| a.name <=> b.name } %> <% for category in @categories.sort { |a,b| a.rank <=> b.rank } %> |
This will let you reorder you objects by name or rank or whatever other attribute you want to order by.
Thanks to Tom Mornini for sharing this tidbit on the mailing group today.
If you aren’t part of the rails mailing list I’d suggest subscribing. There is alot of mail to wade through, but most of it is pretty darn good and helpful.
Comments