title: Populating Cocoon + Autocomplete - error: ActiveRecord::RecordNotFound
body: <p>I'm using these gems and I want to populate a form based on the name of the ingredient. Everything is working. However, when I try to Create the Recipe I get the following error: <br>
  <strong>Couldn't find Ingredient with ID=1 for MealIngredient with ID=</strong>
  <hr />
  <strong>VIEWS:</strong><BR>
  This is the <strong>_form</strong> partial required by Cocoon</p>
  
  <pre><code>&lt;div class = "row"&gt;
    &lt;div class= "col-md-10 col-md-offset-1"&gt;
      &lt;%= simple_form_for(@meal) do |f| %&gt;
        &lt;%= f.error_notification %&gt;
        &lt;div class="form-inputs"&gt;
          &lt;%= f.input :name %&gt;
        &lt;/div&gt;
        &lt;h3&gt;ingredientes&lt;/h3&gt;
        &lt;fieldset id="ingredients"&gt;
          &lt;ol&gt;
            &lt;%= f.fields_for :meal_ingredients do |meal_ingredient| %&gt;
              &lt;%= render "meal_ingredient_fields", :f =&gt; meal_ingredient  %&gt;
            &lt;% end %&gt;
          &lt;/ol&gt;
  
          &lt;div class="form-actions"&gt;
            &lt;div&gt;&lt;%= link_to_add_association 'adicionar ingrediente', f, :meal_ingredients, 'data-association-insertion-node' =&gt; "#ingredients ol", 'data-association-insertion-method' =&gt; "append", :wrap_object =&gt; Proc.new {|quantity| quantity.build_ingredient; quantity }, :class =&gt; "btn btn-default" %&gt;&lt;/div&gt;
          &lt;/div&gt;
        &lt;/fieldset&gt;
        &lt;div class="form-actions"&gt;
          &lt;div&gt;&lt;%= f.button :submit, class: "btn btn-success" %&gt;&lt;/div&gt;
        &lt;/div&gt;
      &lt;% end %&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>This _form render the following <strong>_meal_ingredient_fields</strong> partial</p>
  
  <pre><code>&lt;div class = "nested-fields"&gt;
    &lt;table class= "table"&gt;
        &lt;thead&gt;
          *
          *
          *
        &lt;/thead&gt;
        &lt;tbody&gt;
          &lt;tr&gt;
            &lt;%= f.fields_for :ingredient do |fi| %&gt;
              &lt;%= fi.hidden_field :id %&gt;
            &lt;td scope="row" class="col-md-4"&gt;
              &lt;%= fi.autocomplete_field :name, autocomplete_ingredient_name_ingredients_path, :id_element =&gt; '#ingredient_id', class: "form-control" %&gt;
            &lt;/td&gt;
            &lt;td class="col-md-1"&gt;
              &lt;%= fi.text_field :unit, required: true, class: "form-control unit_input" %&gt;
            &lt;/td&gt;
            &lt;td class="col-md-1"&gt;
              &lt;%= fi.text_field :carb, required: true, :pattern =&gt; '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control carb_input" %&gt;
            &lt;/td&gt;
            &lt;td class="col-md-1"&gt;
              &lt;%= fi.text_field :prot, required: true, :pattern =&gt; '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control prot_input" %&gt;
            &lt;/td&gt;
            &lt;td class="col-md-1"&gt;
              &lt;%= fi.text_field :fat, required: true, :pattern =&gt; '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control fat_input" %&gt;
            &lt;/td&gt;
            &lt;% end %&gt;
            &lt;td class="col-md-1"&gt;
              &lt;%= f.number_field :quantity, required: true, :pattern =&gt; '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control" %&gt;
  
            &lt;/td&gt;          
            &lt;td class="col-md-1"&gt;
            &lt;/td&gt;
            &lt;td class="col-md-1"&gt;
              &lt;%= link_to_remove_association "remove item", f, :class =&gt; "btn btn-danger" %&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><hr />
  <strong>JAVASCRIPT:</strong><br>
  ingredients.js</p>
  
  <pre><code> $(function(){
    var num;
    var id;
    $('#ingredients').on('cocoon:after-insert', function(e, insertedItem) {
      $('input[id^="meal_meal_ingredients_attributes_"][id$="_ingredient_attributes_name"]').each(function(){
          var current = $(this);
          var rx = /meal_meal_ingredients_attributes_(.*)_ingredient_attributes_name/;
          num = rx.exec(current.attr('id'))[1];
          id = '#meal_meal_ingredients_attributes_'+ num +'_ingredient_attributes_';
      });
      $(id + 'name').bind('railsAutocomplete.select', function(event, data){
        $( id + 'id' ).val( data.item.id);
        $( id + 'unit' ).val( data.item.unit).prop('disabled', true);
        $( id + 'carb' ).val( data.item.carb).prop('disabled', true);
        $( id + 'prot' ).val( data.item.prot).prop('disabled', true);
        $( id + 'fat' ).val( data.item.fat).prop('disabled', true);
      });
    });
  }); 
  </code></pre>
  
  <p><em>In case of needing something else, this is my repo: <a href="https://github.com/betinhosaad/inutree" rel="nofollow">https://github.com/betinhosaad/inutree</a></em> <br>
  Thank you!</p>
  
  <p><strong>EDITED</strong><br>
  PARAMS:</p>
  
  <pre><code>[1] pry(#&lt;MealsController&gt;)&gt; params
  =&gt; {"utf8"=&gt;"✓",
   "authenticity_token"=&gt;"h7NGQ35IEbQXHjLc+yLzQC8E6hzfoj7AsjNauPca8Hou6bQ49nuTKjq0g9hgg2myzLw+1P6xXbv35zWUtpLh0Q==",
   "meal"=&gt;
    {"name"=&gt;"Foo",
  =&gt; {"utf8"=&gt;"✓",
   "authenticity_token"=&gt;"h7NGQ35IEbQXHjLc+yLzQC8E6hzfoj7AsjNauPca8Hou6bQ49nuTKjq0g9hgg2myzLw+1P6xXbv35zWUtpLh0Q==",
   "meal"=&gt;
    {"name"=&gt;"Foo",
     "meal_ingredients_attributes"=&gt;
      {"1449865135634"=&gt;{"ingredient_attributes"=&gt;{"id"=&gt;"1", "name"=&gt;"Tapioca"}, "id"=&gt;"", "quantity"=&gt;"2", "_destroy"=&gt;"false"},
       "1449865141143"=&gt;
        {"ingredient_attributes"=&gt;{"id"=&gt;"", "name"=&gt;"Grape", "unit"=&gt;"und", "carb"=&gt;"1", "prot"=&gt;"0", "fat"=&gt;"0"},
         "id"=&gt;"",
         "quantity"=&gt;"4",
         "_destroy"=&gt;"false"}}},
   "commit"=&gt;"Create Meal",
   "controller"=&gt;"meals",
   "action"=&gt;"create"}
  </code></pre>