# Iterate through a table record

**URL:** <https://www.ruby-forum.com/t/iterate-through-a-table-record/153860>\
**Category:** Rails\
**Created:** [November 21, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/iterate-through-a-table-record/153860 "2008-11-21T22:39:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [November 21, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/iterate-through-a-table-record/153860/1 "2008-11-21T22:39:56Z")

</div>

Hello…I’ve searched before posting but can’t find anything. New and  
need help.  
I want to iterate through some fields in my database table and populate  
a html table with their values.

As an example, lets say I have a table (named race) that holds  
information on a relay race. I have fields leg1, leg2…leg36 that hold  
the miles per leg. I want to be able to run through a loop from 1 to 36  
and displays all the legs. I thought this would work:

**Name:**  
\<%=h @race.name %\>  
…

 \<% 36.times do |p| %\> Leg \<%= @race.leg+p %\> \<% end %\>

But I can’t combine @race.leg and p.  
Is there a way to increment @race.leg so I don’t have to write  
@race.leg1 removed\_email\_address@domain.invalid36 ?

Any help appreciate - thanks!  
…Bill

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [November 21, 2008, 11:38pm UTC](https://www.ruby-forum.com/t/iterate-through-a-table-record/153860/2 "2008-11-21T23:38:47Z")

</div>

2008/11/21 Bill M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Is there a way to increment @race.leg so I don’t have to write  
> @race.leg1 removed\_email\_address@domain.invalid36 ?

Can you change the database structure? I think it would make more sense  
to  
have a “legs” table in this case. Then you could lookup all legs of a  
given  
race and iterate through them with @legs.each

So long  
Lennart K.

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [November 21, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/iterate-through-a-table-record/153860/3 "2008-11-21T23:59:12Z")

</div>

Lennart K. wrote:

> 2008/11/21 Bill M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > Is there a way to increment @race.leg so I don’t have to write  
> > @race.leg1 removed\_email\_address@domain.invalid36 ?
> 
> Can you change the database structure? I think it would make more sense  
> to  
> have a “legs” table in this case. Then you could lookup all legs of a  
> given  
> race and iterate through them with @legs.each
> 
> So long  
> Lennart K.

Lennart…interesting thought, problem is I’ll still have the default  
fields I have to deal with (created\_at,updated\_at, race\_id)so it  
wouldn’t be a purely ‘leg’ table. I could filter that out I guess but I  
want to try to make this work.  
Thanks for the reply

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [November 22, 2008, 1:52am UTC](https://www.ruby-forum.com/t/iterate-through-a-table-record/153860/4 "2008-11-22T01:52:17Z")

</div>

A legs table seems more flexible, however:

 \<% (1..36).each do |p| %\> Leg \<%= @race.send(('leg' + p).to\_sym) %\> \<% end %\>

On Nov 21, 5:59 pm, Bill McGuire [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [November 22, 2008, 4:52am UTC](https://www.ruby-forum.com/t/iterate-through-a-table-record/153860/5 "2008-11-22T04:52:14Z")

</div>

Harold wrote:

> A legs table seems more flexible, however:
> 
> \<% (1..36).each do |p| %\> Leg \<%= @race.send(('leg' + p).to\_sym) %\> \<% end %\>
> 
> On Nov 21, 5:59ï¿½pm, Bill McGuire [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Harold…worked just like I wanted. The only thing I had to do was add  
‘.to\_s’ after ‘p’.  
Thanks !
