Search code examples
ruby-on-railsrubymany-to-many

Displaying join table row in view (has_many through association)


The thing is: I'm doing a basic diet planner which has meals, ingredients and quantities. When a user make a meal, he select the ingredients and set the quantity of it. All I want is to display in the meal index page rows with the ingredients and its quantities.

<% meal.ingredients.each do |ingredient| %>
        <tr>
          <td><%= ingredient.name %></td>
          <% ingredient.quantities.each do |q| %>
            <td><%= q.quant %></td>
          <% end %>
          <td><%= ingredient.unit %></td>
          <td><%= ingredient.carb %></td>
          <td><%= ingredient.prot %></td>
          <td><%= ingredient.fat %></td>
        </tr>
      <% end %>

This is showing all quantities associated with the selected ingredient. I'm not posting models because I think it's too obvious. Thanks

EDIT

db/schema.rb

  create_table "ingredients", force: :cascade do |t|
    t.string   "name"
    t.string   "unit"
    t.float    "carb"
    t.float    "prot"
    t.float    "fat"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "meals", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "quantities", force: :cascade do |t|
    t.float   "quant"
    t.integer "ingredient_id"
    t.integer "meal_id"
  end

views/meals/index.html.erb

<% @meals.each do |meal| %>
  <div class="x_panel">
    <div class="x_title">
      <h2><%= meal.name %></h2>
      <ul class="nav navbar-right panel_toolbox">
        <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
        </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
          <ul class="dropdown-menu" role="menu">
            <li><%= link_to 'Show', meal %>
            </li>
            <li><%= link_to 'Edit', edit_meal_path(meal) %>
            </li>
          </ul>
        </li>
        <li><%= link_to '', meal, method: :delete, data: { confirm: 'Are you sure?' }, class: "fa fa-close" %>
        </li>
      </ul>
      <div class="clearfix"></div>
    </div>
    <div class="x_content">

      <table class="table table-striped">

        <tbody>
          <% meal.ingredients.each do |ingredient| %>
            <tr>
              <td><%= ingredient.name %></td>
              <% ingredient.quantities.each do |q| %>
                <td><%= q.quant %></td>
              <% end %>
              <td><%= ingredient.unit %></td>
              <td><%= ingredient.carb %></td>
              <td><%= ingredient.prot %></td>
              <td><%= ingredient.fat %></td>
            </tr>
          <% end %>
        </tbody>
      </table>

    </div>
  </div>
<% end %>

<%= link_to 'New Meal', new_meal_path %>

enter image description here The highlighted numbers are quantities from banana ingredient. However, for the meal it's associated I want only "123". I guess it's a problem of queries and I don't find out how to fix it.


Solution

  • GOT IT!

    <p id="notice"><%= notice %></p>
    <h1>Listing Meals</h1>
    <% @meals.each do |meal| %>
      <div class="x_panel">
        <div class="x_title">
          <h2><%= meal.name %></h2>
          <ul class="nav navbar-right panel_toolbox">
            <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
            </li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
              <ul class="dropdown-menu" role="menu">
                <li><%= link_to 'Show', meal %>
                </li>
                <li><%= link_to 'Edit', edit_meal_path(meal) %>
                </li>
              </ul>
            </li>
            <li><%= link_to '', meal, method: :delete, data: { confirm: 'Are you sure?' }, class: "fa fa-close" %>
            </li>
          </ul>
          <div class="clearfix"></div>
        </div>
        <div class="x_content">
    
          <table class="table table-striped">
    
            <tbody>
              <% meal.ingredients.each do |ingredient| %>
                <tr>
                  <td><%= ingredient.name %></td>
    <!-- FILTERED INGREDIENT QUANTITY ACCORDING TO MEAL ID -->
                  <td><%= ingredient.quantities.find_by(meal_id: meal.id).quant %></td>
                  <td><%= ingredient.unit %></td>
                  <td><%= ingredient.carb %></td>
                  <td><%= ingredient.prot %></td>
                  <td><%= ingredient.fat %></td>
                </tr>
              <% end %>
            </tbody>
          </table>
    
        </div>
      </div>
    <% end %>
    
    <%= link_to 'New Meal', new_meal_path %>
    

    I just don't know if it's the best solution, but worked for me!