2

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">]

1 Answer 1

4

The each should look like this:

<% food_menu.products.each do |product| %>

each returns the enumeration it was called on (the "array", although it's not a real array).

<%= %> renders the value inside it. In this case, the array-looking thing--the return value of each.

Sign up to request clarification or add additional context in comments.

1 Comment

@monfresh All those characters start to look the same after awhile :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.