This has been driving me crazy. I've done lots of google and stackoverflow searches but can't find a solution. I have 2 models: FoodMenu and Product. FoodMenu has_many :products and Product belongs_to :food_menu. The products table has a "food_menu_id" column and I've also done add_index :products, :food_menu_id.
I want to create a view that lists all FoodMenus and their Products. In the views folder I have a pages folder that contains "menus.html.erb". In pages.controller.rb, I have:
def menus
@food_menus = FoodMenu.includes(:products).all
end
In menus.html.erb, I have:
<%= @food_menus.each do |food_menu| %>
<%= food_menu.name %>
<ul>
<%= food_menu.products.each do |product| %>
<li><%= product.name %></li>
<% end %>
</ul>
<% end %>
This returns the expected data, except that after each set of products, it also displays the full array of products, and then at the end, it displays the full array of FoodMenus. Why is it doing that and how can I fix it?
By full array, I mean this: [#FoodMenu id: 1, name: "Sandwiches", description: "right off the press", created_at: "2011-12-21 20:39:42", updated_at: "2011-12-21 20:39:42">, #FoodMenu id: 2, name: "Pizza", description: "fresh local dough", created_at: "2011-12-21 20:40:03", updated_at: "2011-12-21 20:40:03">]