Tyler Gotto
Tyler Gotto

Reputation: 43

adding attributes to join_table from primary form

Hi I have a primary model, recipes with a has_many association with ingredients, which also has_many recipes, my join table is recipes_ingredients, the join table has the columns recipe_id, ingredient_id, and quantity. The quantity is just a string for how many times that ingredient will be used in that recipe.

enter image description here

I have a form that creates the recipe and saves it in the database at the same time it creates and saves any ingredients that are given in the form with a nested attribute... I cannot for the life of me figure out how to add the quantity for this join-table using this same form. I would appreciate any help you can afford me... thank you so much in advance.

    # models/recipe.rb
    class Recipe < ApplicationRecord
      belongs_to :user
      has_many :recipe_ingredients
      has_many :ingredients, through: :recipe_ingredients
      validates :name, :content, :cook_time, presence: true

      def ingredients_attributes=(ingredients_hash)
        ingredients_hash.each do |i, ingredients_attributes|
          if ingredients_attributes[:name].present?
            ingredient = Ingredient.find_or_create_by(name: ingredients_attributes[:name].capitalize!)
            if !self.ingredients.include?(ingredient)
              self.recipe_ingredients.build(:ingredient => ingredient)
            end
          end
       end
     end

 # models/ingredient.rb
 class Ingredient < ApplicationRecord
   has_many :recipe_ingredients
   has_many :recipe, through: :recipe_ingredients
   validates :name, presence: true
 end

 # models/recipe_ingredient.rb
 class RecipeIngredient < ApplicationRecord
   belongs_to :ingredient 
   belongs_to :recipe
 end

   # form
    <%= form_with(model: instruction, local: true) do |form| %>
    <% if instruction.errors.any? %>
    <div id="error_explanation">
    <h4><%= pluralize(instruction.errors.count, "error") %> prohibited 
     this instruction from being saved:</h4>

    <ul>
    <% instruction.errors.full_messages.each do |message| %>
    <li><%= message %></li>
   <% end %>
  </ul>
  </div>
  <% end %>

 <div class="container">
 <div class="row">
 <div class="col-sm-6">
  <h3 class="text-center">Recipe</h3>
  <div class="fields">
    <%= form.label :name %><br>
    <%= form.text_field :name, :placeholder => "Name" %><br>
    <%= form.label "Instructions"  %><br>
    <%= form.text_area :content, :placeholder => "Recipe 
    Instructions" %><br>
    <%= form.label :cook_time %><br>
    <%= form.text_field :cook_time, :placeholder => "(ex,. 45 
    mins)" %><br>
    <%= form.hidden_field :user_id, value: params[:user_id] %>
  </div>
  </div>

   <div class="col-sm-6">
    <h3 class="text-center">Ingredients</h3>
    <div class="row">
      <div class="col-sm-6 checkbox">
            <%= form.collection_check_boxes(:ingredient_ids, 
        Ingredient.all, :id, :name) %> 
        </div>
        <div class="col-sm-6">
            <%= form.fields_for :ingredients do 
        |ingredient_builder| %>
                <%= ingredient_builder.text_field :name %><br>
            <% end %>
          </div>
        </div>
    </div>
   </div>

    <div class="row justify-content-center submit-row">
     <div class="fields text-center">
      <%= form.submit %>
   </div>
  </div>


 </div>[![enter image description here][1]][1]

Upvotes: 1

Views: 61

Answers (1)

Related Questions