best way to manage group by request in rails?

Hey all,
I'm doing something like that:

#in my controller:
@posts=Post.find(:all,:group=>'created_on')

#then in my view:
<% for post in @posts %>
<div id=posts><% 'Posts created on '+post.created_on%>
@currentposts=Post.find_by_created_on(post.created_on)
<% for p in @currentposts %>
<li>p.title</li>
<% end %>
</div>
<% end %>

Any idea how I could get the list of posts that were created on each
date without having to do that Post.find query in my view?

thanx in advance

Pat

Patrick :

Any idea how I could get the list of posts that were created
on each date without having to do that Post.find query in my view?

You can use Enumerable#group_by.
sth like : Post.find(:all).group_by(&:created_on)

   -- Jean-François.

ok thanx a lot, and what if I want to group them by day cause I'm
using datetime which also have hours and second. I would like to group
them by day only. Any idea?

thanx in advance

Pat

You could always do...

@posts = Post.find(:all, :order => 'created_on')

Then in your view..

<% last_day = 0 %>
<% @posts.each do |p| %>
   <% if p.created_on.day != last_day %>
     ... print out a date header or something...
     <% last_day = p.created_on.day %>
   <% end %>
     .... print out p....
<% end %>

This will break if you have consecutive rows in differing months/years
but have the same day, but to get around that change the last_day to
laste_date and set it appropriately and you should be fine.

-philip

You might find Chronic plugin useful, check it out : http://chronic.rubyforge.org/

thanx lot.