title: Trying to use accepts_nested_attributes_for and has_and_belongs_to_many but the join table is not being populated
body: <p>I am learning RoR and trying to use accepts_nested_attributes_for and has_and_belongs_to_many to submit information that would traditionally be two forms.  I have read on some sites they are compatible, some sites they aren't compatible, and some sites don't know.  As reference, I am using Rails 2.3.4.  I tried modeling my solution from  the Ryan's Scraps tutorial on <a href="http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes" rel="noreferrer">nested models</a></p>
  
  <p>From what I have tried to debug, it seems that I have two problems but I am not sure why.  </p>
  
  <ol>
  <li>When I submit a form with nested models, only part of the nested model information is posted.  I only get the first field, not the "n" others the user may have selected</li>
  <li>Of the single field that gets posted, there aren't any rows inserted into the join table that I created for the HABTM relationship.</li>
  </ol>
  
  <p>Here is a piece of code and the corresponding logs for my insertion attempt:</p>
  
  <p>Attorney Model: </p>
  
  <pre><code>class Attorney &lt; ActiveRecord::Base 
    has_and_belongs_to_many :associations
    accepts_nested_attributes_for :associations, :reject_if =&gt; proc { |a| a['name'].blank? }
  end
  </code></pre>
  
  <p>Association Model:</p>
  
  <pre><code>class Association &lt; ActiveRecord::Base
    has_and_belongs_to_many :attorneys
    accepts_nested_attributes_for :attorneys
    validates_presence_of :name, :message =&gt; "Please enter an association name."
  end
  </code></pre>
  
  <p>Attorneys Controller:</p>
  
  <pre><code>def new
    @attorney = Attorney.new
    @attorney.associations.build
  
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml =&gt; @attorney }
    end
  end
  
  def create
    @attorney = Attorney.new(params[:attorney])
  
    respond_to do |format|
      if @attorney.save
        flash[:notice] = 'Attorney was successfully created.'
        format.html { redirect_to(@attorney) }
        format.xml  { render :xml =&gt; @attorney, :status =&gt; :created, :location =&gt; @attorney }
      else
        format.html { render :action =&gt; "new" }
        format.xml  { render :xml =&gt; @attorney.errors, :status =&gt; :unprocessable_entity }
      end
    end
  end
  </code></pre>
  
  <p>Attorney's New View:</p>
  
  <pre><code>&lt;% form_for(@attorney, :html =&gt; {:multipart =&gt; true}) do |f| %&gt;
    &lt;%= f.error_messages %&gt;
   &lt;%= f.label :"First name" %&gt; 
   &lt;%= f.text_field :firstname %&gt;&lt;br&gt;
  
   &lt;%= f.label :"Last Name" %&gt; 
   &lt;%= f.text_field :lastname %&gt;&lt;br&gt;
  
   &lt;%= f.label :"Attorney Type" %&gt; 
   &lt;%= f.collection_select :member_type_id, MemberType.all, :id, :name %&gt;&lt;br&gt;
  
   &lt;%= f.text_area :bio, :cols =&gt; 70, :rows =&gt; 20 %&gt;&lt;br&gt;&lt;br&gt;
  
   &lt;%= f.label :"Attorney Location" %&gt; 
   &lt;%= f.collection_select :office_location_id, OfficeLocation.all, :id, :location %&gt;&lt;br&gt;
  
   &lt;div id="associations"&gt;
        &lt;%= render :partial =&gt; 'shared/membership' %&gt;
   &lt;/div&gt;
   &lt;%= add_association_link "Add Association" %&gt;
      &lt;%= f.submit 'Create' %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Membership Partial:</p>
  
  <pre><code>&lt;div class="association"&gt;
    &lt;% fields_for :associations do |assoc_form| %&gt;
      &lt;%= assoc_form.collection_select(:association_id, Association.find(:all), :id, :name, :include_blank =&gt; true) %&gt;
  </code></pre>
  
  <p>&lt;%= link_to_function "remove", "$(this).up('.association').remove()" %>
   &lt;%= link_to 'New Association', new_association_path %>
        &lt;% end %>
      </p>
  
  <p>Attorney Helper Link:</p>
  
  <pre><code>def add_association_link(name)
    link_to_function name do |page|
      page.insert_html :bottom, :associations, :partial =&gt; 'shared/membership', :object =&gt; AssociationsAttorneys.new
    end
  end
  </code></pre>
  
  <p>Join Table Migration:</p>
  
  <pre><code>class CreateAssociationsAttorneys &lt; ActiveRecord::Migration
    def self.up
      create_table :associations_attorneys do |t|
        t.references :attorney, :null =&gt; false
        t.references :association, :null =&gt; false
        t.timestamps
      end
    end
  
    def self.down
      drop_table :associations_attorneys
    end
  end
  </code></pre>
  
  <p>Log capture:</p>
  
  <pre><code>    Processing AttorneysController#new (for 127.0.0.1 at 2009-12-04 08:16:19) [GET]
  Rendering template within layouts/default
  Rendering attorneys/new
    [4;35;1mMemberType Load (0.4ms)[0m   [0mSELECT * FROM "member_types" [0m
    [4;36;1mOfficeLocation Load (18.6ms)[0m   [0;1mSELECT * FROM "office_locations" [0m
    [4;35;1mAssociation Load (0.6ms)[0m   [0mSELECT * FROM "associations" [0m
  Rendered shared/_membership (3.5ms)
    [4;36;1mCACHE (0.0ms)[0m   [0;1mSELECT * FROM "associations" [0m
  Rendered shared/_membership (1.5ms)
  Rendered shared/_nav (0.6ms)
  Rendered shared/_footer (0.1ms)
  Completed in 149ms (View: 114, DB: 20) | 200 OK [http://localhost/attorneys/new]
  
  Processing ApplicationController#index (for 127.0.0.1 at 2009-12-04 08:16:19) [GET]
  
  Processing AttorneysController#create (for 127.0.0.1 at 2009-12-04 08:16:57) [POST]
    Parameters: {"commit"=&gt;"Create", "authenticity_token"=&gt;"Jh7aMCcOY7jUu/D1YtiCswg2n6iwqnS98VnVn46psp0=", "associations"=&gt;{"association_id"=&gt;"3"}, "attorney"=&gt;{"birthstate"=&gt;"Alabama", "office_location_id"=&gt;"1", "birthdate"=&gt;"December 3, 2009", "birthcity"=&gt;"Test", "middlename"=&gt;"Test", "lastname"=&gt;"Testing", "image_temp"=&gt;"", "member_type_id"=&gt;"2", "firstname"=&gt;"Test", "bio"=&gt;"testing testing testing", "suffix"=&gt;"", "email"=&gt;"testing@test.com"}}
    [4;35;1mAttorney Load (15.6ms)[0m   [0mSELECT "attorneys".id FROM "attorneys" WHERE ("attorneys"."email" = 'testing@test.com') LIMIT 1[0m
    [4;36;1mAttorney Create (0.8ms)[0m   [0;1mINSERT INTO "attorneys" ("birthstate", "created_at", "birthdate", "office_location_id", "birthcity", "updated_at", "middlename", "lastname", "firstname", "member_type_id", "suffix", "bio", "image", "email") VALUES('Alabama', '2009-12-04 15:16:57', 'December 3, 2009', 1, 'Test', '2009-12-04 15:16:57', 'Test', 'Testing', 'Test', 2, '', 'testing testing testing', NULL, 'testing@test.com')[0m
  Redirected to http://localhost:3000/attorneys/11
  Completed in 150ms (DB: 16) | 302 Found [http://localhost/attorneys]
  </code></pre>
  
  <p>I can see that associations"=>{"association_id"=>"3"} it is only getting the last of the multiple associations that I had for the particular person and it isn't creating any entries into the join table.  Where might my code have gone wrong?</p>