# Getting the last record for an each loop?

**URL:** <https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891>\
**Category:** Rails\
**Created:** [November 13, 2009, 2:58am UTC](https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891 "2009-11-13T02:58:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [November 13, 2009, 2:58am UTC](https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891/1 "2009-11-13T02:58:01Z")

</div>

Hi all,

I need a very similar functionality to what is displayed on the group\_by  
Month railscast ([#29 group\_by Month - RailsCasts](http://railscasts.com/episodes/29-group-by-month)).

The code is this…

\<% @task\_months.sort.each do |month, tasks| %\>

## \<%= month.strftime('%B') %\>
 \<% for task in tasks %\> 
**\<%= task.name %\>** due on \<%= task.due\_at.to\_date.to\_s(:long) %\> 
 \<% end %\> 
* * *

\<% end %\>

The HR tag above is what I need to not show in the page. I know I could  
this easily in a for loop with something like:

\<% for task in tasks %\>  
\<%= ‘

* * *
’ unless task == tasks.last %\>  
…

Anyone have any clue how to do this with an each loop?

Many thanks!  
-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [November 13, 2009, 4:20am UTC](https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891/2 "2009-11-13T04:20:46Z")

</div>

tasks.each\_with\_index do |task, index|  
…  
\<%= content\_tag(:hr) unless index == tasks.size %\>

might need a +1 on either of index or .size, but that’s the idea.

-eric

On Nov 12, 5:58 pm, Tony T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [November 13, 2009, 4:21am UTC](https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891/3 "2009-11-13T04:21:57Z")

</div>

On Thu, Nov 12, 2009 at 5:58 PM, Tony T.  
\<removed\_email\_address@domain.invalid

> wrote:

> ## \<%= month.strftime('%B') %\>
> 
> Many thanks!  
> -Tony

Tony, you should be able to do something like this:

\<% for task in tasks %\>

…

\<% tag :hr unless task == tasks.last %\>  
\<% end %\>

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [November 13, 2009, 4:29am UTC](https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891/4 "2009-11-13T04:29:34Z")

</div>

On Thu, Nov 12, 2009 at 7:20 PM, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > The code is this…
> > 
> > * * *
> 
> \<% for task in tasks %\>
> 
> …
> 
> \<% tag :hr unless task == tasks.last %\>

The above should be

\<%= tag :hr unless task == tasks.last %\>

-Conrad

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [November 13, 2009, 4:48am UTC](https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891/5 "2009-11-13T04:48:44Z")

</div>

Conrad T. wrote:

> On Thu, Nov 12, 2009 at 7:20 PM, Conrad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > The code is this…
> > > 
> > > * * *
> > 
> > \<% for task in tasks %\>
> > 
> > …
> > 
> > \<% tag :hr unless task == tasks.last %\>
> 
> The above should be
> 
> \<%= tag :hr unless task == tasks.last %\>
> 
> -Conrad

Thanks for the reply guys… I’ve tried doing all of the suggestions but  
it still doesn’t work. I suspect it may be something to do with the sort  
call?

\<% @task\_months.sort.each do |month, tasks| %\>

By the way - The issue is with the each loop, not the for loop.

Thanks again!  
-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [November 13, 2009, 7:42pm UTC](https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891/6 "2009-11-13T19:42:01Z")

</div>

Conrad T. wrote:

> On Thu, Nov 12, 2009 at 5:58 PM, Tony T.  
> \<removed\_email\_address@domain.invalid
> 
> > wrote:
> 
> > ## \<%= month.strftime('%B') %\>
> > 
> > Many thanks!  
> > -Tony
> 
> Tony, you should be able to do something like this:
> 
> \<% for task in tasks %\>
> 
> …
> 
> \<% tag :hr unless task == tasks.last %\>  
> \<% end %\>

Better:

\<% tasks.collect do |task| %\>  
HTML markup  
\<% end.join tag(:hr) %\>

(some % may need to be %=)

Better still: refactor the whole thing into partials and/or helpers.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

> Good luck,
> 
> -Conrad

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [November 13, 2009, 11:32am UTC](https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891/7 "2009-11-13T11:32:11Z")

</div>

2009/11/13 Tony T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > Â Â …
> 
> Thanks for the reply guys… I’ve tried doing all of the suggestions but  
> it still doesn’t work. I suspect it may be something to do with the sort  
> call?
> 
> \<% @task\_months.sort.each do |month, tasks| %\>
> 
> By the way - The issue is with the each loop, not the for loop.

Have a look at the rails guide on Debugging and then break in to the  
loop and work out what is going on. (use ruby-debug). You can inspect  
each variable and see what is not as you expect.

Colin

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [November 13, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/getting-the-last-record-for-an-each-loop/178891/8 "2009-11-13T19:44:23Z")

</div>

Tony T. wrote:

> Hi all,
> 
> I need a very similar functionality to what is displayed on the group\_by  
> Month railscast ([http://railscasts.com/episodes/29-group-by-month](http://railscasts.com/episodes/29-group-by-month)).
> 
> The code is this…
> 
> \<% @task\_months.sort.each do |month, tasks| %\>
> 
> ## \<%= month.strftime('%B') %\>
> \<% for task in tasks %\> 
> **\<%= task.name %\>** due on \<%= task.due\_at.to\_date.to\_s(:long) %\> 
> \<% end %\> 
> * * *
> 
> \<% end %\>

In addition to my other suggestion, you might consider using a

  
element instead of all the 
* * *
s. Since this is a table of data, the  
semantics are more appropriate to the 

 element.

> The HR tag above is what I need to not show in the page. I know I could  
> this easily in a for loop with something like:
> 
> \<% for task in tasks %\>  
> \<%= ‘
> 
> * * *
> ’ unless task == tasks.last %\>  
> …
> 
> Anyone have any clue how to do this with an each loop?
> 
> Many thanks!  
> -Tony

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
