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 %>

<% 'Posts created on '+post.created_on%> @currentposts=Post.find_by_created_on(post.created_on) <% for p in @currentposts %>
  • p.title
  • <% end %>
    <% 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.


    À la renverse.

    On 12/4/06, Jean-François [email protected] wrote:

    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)

    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

    Patrick A. wrote:

    On 12/4/06, Jean-Fran�ois [email protected] wrote:

    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)

    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

    Post.find(:all).group_by { |post| post.created_on.strftime('%Y-%m-%d) }

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

    On 12/5/06, Alex W. [email protected] wrote:

    Post.find(:all).group_by { |post| post.created_on.strftime('%Y-%m-%d) }

    thanx lot.