# Inserting column headings between grouped rows

**URL:** <https://www.ruby-forum.com/t/inserting-column-headings-between-grouped-rows/97092>\
**Category:** Rails\
**Created:** [January 25, 2006, 7:49am UTC](https://www.ruby-forum.com/t/inserting-column-headings-between-grouped-rows/97092 "2006-01-25T07:49:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parker\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eada6e/32.png) [@Parker\_T](https://www.ruby-forum.com/u/Parker_T)\
**Post date:** [January 25, 2006, 7:49am UTC](https://www.ruby-forum.com/t/inserting-column-headings-between-grouped-rows/97092/1 "2006-01-25T07:49:30Z")

</div>

Hello all,

I have what is conceptually a pretty simple poblem, but can’t think of  
an obvious solution. I have a group of objects (@items), that I wish  
to render as rows. Items have dates, and I wish to group them by that  
date, inserting a header row each time the date changes e.g.:

header row  
row 11/11/2004  
row 11/11/2004  
header row  
row 11/23/2004

I’m currently rendering the rows like so:

render :partial =\> ‘item’, :collection =\> @items

I’m curious if there’s a a way to keep track of a date value between  
insert a spacer each time it changes. I couldn’t find one looking  
over the docs. Anyone had to deal with this?

As always, any help is greatly apreciated,

## pt.

Parker T.  
[http://www.parkert.com/](http://www.parkert.com/)  
510.541.0125

---

<div class="post-metadata">

**Author:** ![Parker\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eada6e/32.png) [@Parker\_T](https://www.ruby-forum.com/u/Parker_T)\
**Post date:** [January 25, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/inserting-column-headings-between-grouped-rows/97092/2 "2006-01-25T15:28:48Z")

</div>

Parker:

I hope someone will offer one of those super-efficient solutions. For  
now, I’d do what you want without using the partial. I’d do:

@items.each do |item|

then I could just check the date field against the previous entry and  
use an if block to display the row header when the date changes.

That said, there is probably something much better. Thanks for the  
question, I look forward to the answer.

bruce

---

<div class="post-metadata">

**Author:** ![Parker\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eada6e/32.png) [@Parker\_T](https://www.ruby-forum.com/u/Parker_T)\
**Post date:** [January 25, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/inserting-column-headings-between-grouped-rows/97092/3 "2006-01-25T15:56:26Z")

</div>

Might not be really flashy, but it works:

current = nil  
@items.each do |item|  
if(current != item)  
print\_your\_row\_header()  
current = item  
end  
print\_the\_item()  
end
