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
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 %>
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/