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: <b>Name:</b> <%=h @race.name %> ..... <tr> <% 36.times do |p| %> <td>Leg <%= @race.leg+p %></td> <% end %> </tr> 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 ...@race.leg36 ? Any help appreciate - thanks! ...Bill -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
2008/11/21 Bill Mcguire <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> Is there a way to increment @race.leg so I don''t have to write > @race.leg1 ...@race.leg36 ? >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 Koopmann --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Lennart Koopmann wrote:> 2008/11/21 Bill Mcguire <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > >> Is there a way to increment @race.leg so I don''t have to write >> @race.leg1 ...@race.leg36 ? >> > > 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 KoopmannLennart...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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
A legs table seems more flexible, however: <tr> <% (1..36).each do |p| %> <td>Leg <%= @race.send((''leg'' + p).to_sym) %></td> <% end %> </tr> On Nov 21, 5:59 pm, Bill McGuire <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Lennart Koopmann wrote: > > 2008/11/21 Bill Mcguire <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > >> Is there a way to increment @race.leg so I don''t have to write > >> @race.leg1 ....@race.leg36 ? > > > 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 Koopmann > > 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Harold wrote:> A legs table seems more flexible, however: > > <tr> > <% (1..36).each do |p| %> > <td>Leg <%= @race.send((''leg'' + p).to_sym) %></td> > <% end %> > </tr> > > On Nov 21, 5:59�pm, Bill McGuire <rails-mailing-l...-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt>Harold...worked just like I wanted. The only thing I had to do was add ''.to_s'' after ''p''. Thanks ! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---