Hi there, I have a one to many relationship between a restaurant and menus. In my view I want to iterate through each of the menu''s belonging to a given restaurant. I also need to distinguish between the first one in the list and the rest of them. What is the best control structure/ method for accomplishing this? Assume I have @restaurant.menus to get the list thanks, steve -- 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 -~----------~----~----~----~------~----~------~--~---
Steve Glaz wrote:> Hi there, > > I have a one to many relationship between a restaurant and menus. In my > view I want to iterate through each of the menu''s belonging to a given > restaurant. I also need to distinguish between the first one in the list > and the rest of them. > > What is the best control structure/ method for accomplishing this? > Assume I have @restaurant.menus to get the list > > thanks, > stevefeels like a case of <% @restaurant.menus.each_with_index |menu, index| %> * do stuff for all the menus - call a partial probably * <% if index == 0 %> * do special stuff for the first one * <% else %> * for stuff for the others * <% end %> <% end %> -- 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 -~----------~----~----~----~------~----~------~--~---
That''s yucky! I''d see the use of Array#shift rather than have the conditional code executing on every iteration. <% @menus = @restaurant.menus; @menu = @menus.shift %> <%# do stuff for the first menu %> <% for @menu in @menus %> <%# do stuff for others %> <% end %> Julian Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #4 coming soon! http://sensei.zenunit.com/ On 05/05/2008, at 10:32 AM, Matthew Rudy Jacobs wrote:> > Steve Glaz wrote: >> Hi there, >> >> I have a one to many relationship between a restaurant and menus. >> In my >> view I want to iterate through each of the menu''s belonging to a >> given >> restaurant. I also need to distinguish between the first one in the >> list >> and the rest of them. >> >> What is the best control structure/ method for accomplishing this? >> Assume I have @restaurant.menus to get the list >> >> thanks, >> steve > > feels like a case of > > <% @restaurant.menus.each_with_index |menu, index| %> > * do stuff for all the menus - call a partial probably * > <% if index == 0 %> > * do special stuff for the first one * > <% else %> > * for stuff for the others * > <% end %> > <% end %> > > -- > 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 -~----------~----~----~----~------~----~------~--~---