# Newbie Question

**URL:** <https://www.ruby-forum.com/t/newbie-question/136632>\
**Category:** Rails\
**Created:** [May 4, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/newbie-question/136632 "2008-05-04T22:10:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [May 4, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/newbie-question/136632/1 "2008-05-04T22:10:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [May 5, 2008, 2:32am UTC](https://www.ruby-forum.com/t/newbie-question/136632/2 "2008-05-05T02:32:47Z")

</div>

Steve G. 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 %\>

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [May 5, 2008, 5:52am UTC](https://www.ruby-forum.com/t/newbie-question/136632/3 "2008-05-05T05:52:30Z")

</div>

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/](http://sensei.zenunit.com/)
