title: ActiveRecord not setting foreign key on has_many association using .build
body: <p>I have a Rails app with the following (simplified) models:</p>
  
  <pre><code># member.rb
  class Member &lt; ActiveRecord::Base
    has_many :member_roles, :dependent =&gt; :destroy, 
             :autosave =&gt; true, :inverse_of =&gt; :member
  end
  </code></pre>
  
  <p>...</p>
  
  <pre><code># member_role.rb
  class MemberRole &lt; ActiveRecord::Base
    belongs_to :member, :inverse_of =&gt; :member_roles
    validates_presence_of :member_id
  end
  </code></pre>
  
  <p>If I try to use the <code>.build</code> method on the association, the created object doesn't have the foreign key set.  This causes it to fail validation, or without the validation become not be associated with the <code>Member</code>.</p>
  
  <pre><code># Rails console
  &gt; m = Member.find(280)
  &gt; mr = m.member_roles.build(:role_id =&gt; Role.find_by_name("Crew Chief").id)
  =&gt; #&lt;MemberRole id: nil, member_id: nil, role_id: 6697350, start_date: nil, \
     end_date: nil, memo: nil, created_at: nil, updated_at: nil&gt;
  &gt; mr.save!
  ActiveRecord::RecordInvalid: Validation failed: Member can't be blank
  &gt; mr.save(:validate =&gt; false)
  &gt; mr
  =&gt; #&lt;MemberRole id: 1834, member_id: nil, role_id: 6697350, start_date: nil, \
     end_date: nil, memo: nil, created_at: "2012-04-11 06:37:00", \
     updated_at: "2012-04-11 06:37:00"&gt;
  </code></pre>
  
  <p>Which conflicts with the <a href="http://guides.rubyonrails.org/association_basics.html" rel="nofollow">Rails Guide</a> :</p>
  
  <blockquote>
    <p>The collection.build method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will not yet be saved.</p>
  </blockquote>
  
  <p>Obviously, manually setting the member_id is a easy work around.  But I'd like to avoid that.  I believe this code worked correctly in previous versions of rails;the above is the behavior of Rails 3.2.3.</p>