# Listing entries under corresponding date

**URL:** <https://www.ruby-forum.com/t/listing-entries-under-corresponding-date/141823>\
**Category:** Rails\
**Created:** [June 30, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/listing-entries-under-corresponding-date/141823 "2008-06-30T20:55:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [June 30, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/listing-entries-under-corresponding-date/141823/1 "2008-06-30T20:55:35Z")

</div>

I have a rails app with view code long in need of an overhaul. Some  
of the first app code I wrote was designed to display entires  
belonging to a given date, under a date header derived from a model  
timestamp field. For each day, I list the corresponding entries for  
that day under the date as a header. Here’s an example:

07/09/1976  
— Entry 1  
— Entry 2  
— Entry 3

07/08/1976  
— Entry 1  
— Entry 2

The results are in a @diary object which I loop through. I’d be happy  
to post my code, which is ugly, though i wanted to find out if there  
was a helper to do this, or if there’s a technique that people use to  
easily:

1. extract the date from a timestamp, and write it as a header
2. list all the corresponding entries for that day underneath it

…etc. etc.

Any techniques out there to use? …or are there any helpers I might  
not know about to make this work?

Thanks for your help in advance.

-A

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [June 30, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/listing-entries-under-corresponding-date/141823/2 "2008-06-30T21:32:13Z")

</div>

On Jun 30, 2008, at 11:55 AM, ressister wrote:

> — Entry 3
> 
> 1. extract the date from a timestamp, and write it as a header
> 2. list all the corresponding entries for that day underneath it
> 
> …etc. etc.
> 
> Any techniques out there to use? …or are there any helpers I might  
> not know about to make this work?

Google for “group\_by”. Here’s some examples.

> **[New in Rails: Enumerable#group\_by and Array#in\_groups\_of](https://rubyonrails.org/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of)**
>
> Changeset 3726 adds two little methods to ActiveSupport: Enumerable#group\_by and @ Array#in\_groups\_of@.Enumerable#group\_by is for collecting an enumerable into sets, grouped by the result of a block. Useful, for example, for grouping records by date....

> **[ruby group\_by](http://justbarebones.blogspot.com/2007/10/ruby-groupby.html)**
>
> I often need to convert an array of objects into a hash, indexed by some attribute of the objects. For e.g. for a task list, each task is an...

---

<div class="post-metadata">

**Author:** ![ressister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@ressister](https://www.ruby-forum.com/u/ressister)\
**Post date:** [June 30, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/listing-entries-under-corresponding-date/141823/3 "2008-06-30T21:49:49Z")

</div>

Awesome Phillip, this is very helpful, thanks.

-A
