# Group By

**URL:** <https://www.ruby-forum.com/t/group-by/95365>\
**Category:** Rails\
**Created:** [November 14, 2005, 4:48am UTC](https://www.ruby-forum.com/t/group-by/95365 "2005-11-14T04:48:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 14, 2005, 4:48am UTC](https://www.ruby-forum.com/t/group-by/95365/1 "2005-11-14T04:48:05Z")

</div>

Is there a way to use group by within find without doing find\_by\_sql?

I am trying to group all post by date… showing the date and then  
all the posts by it… then the next date. Anyone have any code  
snippets for this?

Thanks

John K.

–  
[http://www.soen.info](http://www.soen.info) - where software engineering knowledge gets indexed  
[http://cusec.soen.info](http://cusec.soen.info) - software engineering conference

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 14, 2005, 5:27am UTC](https://www.ruby-forum.com/t/group-by/95365/2 "2005-11-14T05:27:10Z")

</div>

Have you taken a look at the Calculations plugin?

[http://techno-weenie.net/blog/code/269/more-on-activerecord-calculations](http://techno-weenie.net/blog/code/269/more-on-activerecord-calculations)

I haven’t used it yet, but it may help you out. Looks really  
interesting.

–Ryan

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 14, 2005, 6:15am UTC](https://www.ruby-forum.com/t/group-by/95365/3 "2005-11-14T06:15:16Z")

</div>

On 11/13/05, Ryan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Have you taken a look at the Calculations plugin?
> 
> [http://techno-weenie.net/blog/code/269/more-on-activerecord-calculations](http://techno-weenie.net/blog/code/269/more-on-activerecord-calculations)
> 
> I haven’t used it yet, but it may help you out. Looks really interesting.

That probably won’t work. It’s mainly for aggregate queries.

Another option is to group them by date yourself:

@posts = Post.find :all

@grouped\_posts = @posts.inject({}) do |all\_posts, post|  
(all\_posts[post.created\_at.to\_date] ||= []) \<\< post  
all\_posts  
end

Then in the view:

# sorts chronologically

\<% @grouped\_posts.keys.sort.each do |date| -%\>

## \<%=h date.to\_s(:long) %\>

 \<% @grouped\_posts[date].each do |post| -%\> - \<%=h post.title %\>
 \<% end -%\> 
\<% end -%\> -- rick http://techno-weenie.net

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 15, 2005, 4:30am UTC](https://www.ruby-forum.com/t/group-by/95365/4 "2005-11-15T04:30:17Z")

</div>

works great! 🙂 thanks
