Thursday, May 7, 2009

Render partial sort results Ruby on Rails

I've been following this excellent tutorial about how to create a blog using ruby on rails in 15 minutes, but when it came to how to sort the comments, I was a little bit disapointed.
Having comments sorted from the oldest to the most recent is not something that I was pleased with.
After searching on the web I didn't find much solutions, so I asked for help railforum.com and here is something that hopefully will help you.
If you just want to order your items by id DESC then do :
<%= render :partial => post.comments.reverse  %>
If you want to pass a parameter to order by then :


<%= render :partial => post.comments.sort_by { |c| c.title} %>
<%= render :partial => post.comments.sort_by { |c| c.created_at} %>

and you can even reverse them


<%= render :partial => post.comments.sort_by { |c| c.title}.reverse %>



Hope this help.
Martin

No comments:

Post a Comment