# Best way to manage group by request in rails?

**URL:** <https://www.ruby-forum.com/t/best-way-to-manage-group-by-request-in-rails/78271>\
**Category:** Rails\
**Created:** [December 4, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/best-way-to-manage-group-by-request-in-rails/78271 "2006-12-04T19:12:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [December 4, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/best-way-to-manage-group-by-request-in-rails/78271/1 "2006-12-04T19:12:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [December 4, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/best-way-to-manage-group-by-request-in-rails/78271/2 "2006-12-04T20:06:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [December 4, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/best-way-to-manage-group-by-request-in-rails/78271/3 "2006-12-04T21:08:59Z")

</div>

On 12/4/06, Jean-François [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [December 5, 2006, 8:51am UTC](https://www.ruby-forum.com/t/best-way-to-manage-group-by-request-in-rails/78271/4 "2006-12-05T08:51:18Z")

</div>

Patrick A. wrote:

> On 12/4/06, Jean-Franï¿½ois [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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) }

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [December 4, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/best-way-to-manage-group-by-request-in-rails/78271/5 "2006-12-04T22:37:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [December 5, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/best-way-to-manage-group-by-request-in-rails/78271/6 "2006-12-05T17:09:38Z")

</div>

On 12/5/06, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

> Post.find(:all).group\_by { |post| post.created\_on.strftime('%Y-%m-%d) }

thanx lot.
