title: using has_many :through and build
body: <p>i have three models, all for a has_many :through relationship. They look like this:</p>
  
  <pre><code>class Company &lt; ActiveRecord::Base
  
    has_many :company_users, dependent: :destroy
    has_many :users, through: :company_users
  
    accepts_nested_attributes_for :company_users, :users
  
  end
  
  class CompanyUser &lt; ActiveRecord::Base
    self.table_name = :companies_users #this is because this was originally a habtm relationship
    belongs_to :company
    belongs_to :user
  end
  
  class User &lt; ActiveRecord::Base
    # this is a devise model, if that matters
  
    has_many :company_users, dependent: :destroy
    has_many :companies, through: :company_users
  
    accepts_nested_attributes_for :company_users, :companies
  
  end
  </code></pre>
  
  <p>this loads fine, and the joins are built fine for queries. However, whenever i do something like</p>
  
  <pre><code>@company = Company.last
  @user = @company.users.build(params[:user])
  
  @user.save    #=&gt; true
  @company.save #=&gt; true
  </code></pre>
  
  <p>both the <code>User</code> record and the <code>CompanyUser</code> records get created, but the <code>company_id</code> field in the <code>CompanyUser</code> record is set to <code>NULL</code></p>
  
  <pre><code>INSERT INTO `companies_users` (`company_id`, `created_at`,`updated_at`, `user_id`) 
  VALUES (NULL, '2012-02-19 02:09:04', '2012-02-19 02:09:04', 18)
  </code></pre>
  
  <p>it does the same thing when you <code>@company.users &lt;&lt; @user</code></p>
  
  <p>I'm sure that I'm doing something stupid here, I just don't know what.</p>