title: Rails 4, double-nested models and strong parameters
body: <p>I am fairly new to Ruby on Rails and began with rails 4 right away.</p>
  
  <p>I have sucessfully nested a Recipe and a Ingredient model so that they can be added in the same form. Next I want to nest quantity within ingredient so that that aswell can be added within the same form. Everything seems to be working fine up until when the quantity of the ingredient is about to be inserted in the database and from this i believe there is something wrong with the strong params in the recipes_controller. But i will post the full code below.
  I am using simple_form for the forms.</p>
  
  <p>Thankful for any help!</p>
  
  <p>Here are my models:</p>
  
  <pre><code>class Recipe &lt; ActiveRecord::Base
    has_many :comments, dependent: :destroy
    has_many :ingredients, dependent: :destroy
    accepts_nested_attributes_for :ingredients, :reject_if =&gt; lambda { |a| a[:name].blank? }, :allow_destroy =&gt; true
    validates :title, presence: true
    validates :desc, presence: true
  end
  
  
  class Ingredient &lt; ActiveRecord::Base
    belongs_to :recipe
    has_many :quantities, dependent: :destroy
    accepts_nested_attributes_for :quantities, :reject_if =&gt; lambda { |a| a[:name].blank? }, :allow_destroy =&gt; true
  end
  
  class Quantity &lt; ActiveRecord::Base
    belongs_to :ingredient
  end
  </code></pre>
  
  <p>Here is the recipes_controller</p>
  
  <pre><code>class RecipesController &lt; ApplicationController
  
    def new
      @recipe = Recipe.new
      3.times do 
        ingredient = @recipe.ingredients.build
        1.times {ingredient.quantities.build }
      end
    end
  
    def create
      @recipe = Recipe.new(params[:recipe].permit(:title, :desc, ingredients_attributes: [:id, :recipe_id, :name, :_destroy, quantities_attributes: [:id, :ingredient_id, :amount, :unit, :_destroy]]))
  
      if @recipe.save
        redirect_to @recipe
      else
        render "new"
      end
    end
  
    def show
      @recipe = Recipe.find(params[:id])
    end
  
  
    def edit
      @recipe = Recipe.find(params[:id])
    end
  
  
    def update
      @recipe = Recipe.find(params[:id])
  
      if @recipe.update(params[:recipe].permit(:title, :desc))
        redirect_to @recipe
      else
        render 'edit'
      end
    end
  
  
    def destroy
      @recipe = Recipe.find(params[:id])
      @recipe.destroy
  
      redirect_to recipes_path
    end
  
  
  
  
  
    def index
      @recipes = Recipe.all
    end
  
    private
      def post_params
        params.require(:recipe).permit(:title, :desc, ingredients_attributes: [:id, :recipe_id, :name, :_destroy, quantities_attributes: [:id, :ingredient_id, :amount, :unit, :_destroy]])
      end
  end
  </code></pre>
  
  <p>Then i use simple form to create a form for recipe, ingredient and quantity through partials.</p>
  
  <p>_form:</p>
  
  <pre><code>&lt;%= simple_form_for @recipe do |f| %&gt;
      &lt;%= f.error_notification %&gt;
  
  
      &lt;%= f.input :title %&gt;
      &lt;%= f.input :desc %&gt;
  
  
  
      &lt;%= f.simple_fields_for :ingredients do |builder| %&gt;
  
      &lt;%= render "ingredient_fields", :f =&gt; builder %&gt;
  
  
      &lt;% end %&gt;
  
      &lt;p class="links"&gt;
      &lt;%= link_to_add_association 'add ingredient', f, :ingredients %&gt;
      &lt;p class="links"&gt;
      &lt;%= f.error :base%&gt;
      &lt;%= f.submit %&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>Which renders from _ingredients_fields:</p>
  
  <pre><code>&lt;div class="nested-fields"&gt;
      &lt;%= f.input :name, label: "Ingredient" %&gt;
  
  
  
      &lt;%= f.simple_fields_for :quantities do |builder| %&gt;
  
      &lt;%= render "quantities_fields", :f =&gt; builder %&gt;
  
      &lt;% end %&gt;
  
      &lt;%= link_to_remove_association "remove", f %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>which renders from _quantities_fields: <strong>[EDITED]</strong></p>
  
  <pre><code>&lt;div class="nested-fields"&gt;
      &lt;%= f.input :amount %&gt;
      &lt;%= f.input :unit %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Trying to add new recipes result in the following log statement:</p>
  
  <pre><code>Started POST "/recipes" for 127.0.0.1 at 2013-10-29 14:15:40 +0100
  Processing by RecipesController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"t6LKgDLwAxaU9xo2ipyCM+j1yfVF9WrI8AoGTX+gRkw=", "recipe"=&gt;{"title"=&gt;"Pancakes", "desc"=&gt;"Tasty", "ingredients_attributes"=&gt;{"0"=&gt;{"name"=&gt;"Milk", "quantities_attributes"=&gt;{"0"=&gt;{"amount"=&gt;"1", "unit"=&gt;"Cup"}}, "_destroy"=&gt;"false"}}}, "commit"=&gt;"Create Recipe"}
    [1m[35m (0.1ms)[0m  begin transaction
    [1m[36mSQL (3.5ms)[0m  [1mINSERT INTO "recipes" ("created_at", "desc", "title", "updated_at") VALUES (?, ?, ?, ?)[0m  [["created_at", Tue, 29 Oct 2013 13:15:40 UTC +00:00], ["desc", "Tasty"], ["title", "Pancakes"], ["updated_at", Tue, 29 Oct 2013 13:15:40 UTC +00:00]]
    [1m[35mSQL (0.4ms)[0m  INSERT INTO "ingredients" ("created_at", "name", "recipe_id", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Tue, 29 Oct 2013 13:15:40 UTC +00:00], ["name", "Milk"], ["recipe_id", 27], ["updated_at", Tue, 29 Oct 2013 13:15:40 UTC +00:00]]
    [1m[36m (7.8ms)[0m  [1mcommit transaction[0m
  Redirected to http://www.projectcookbook.dev/recipes/27
  Completed 302 Found in 22ms (ActiveRecord: 11.7ms)
  </code></pre>