title: Saving belongs_to data when creating new record
body: <pre><code>class Party &lt; ActiveRecord::Base
    belongs_to :hostess, class_name: 'Person', foreign_key: 'hostess_id'
    validates_presence_of :hostess
  end
  
  class Person &lt; ActiveRecord::Base
      has_many :parties, foreign_key: :hostess_id
  end
  </code></pre>
  
  <p>When creating a new <code>Party</code>, the view lets the user select an existing <code>Hostess</code>, or enter a new one. (This is done with jQuery autocomplete to look up existing records.) If an existing record is chosen, <code>params[:party][:hostess_id]</code> will have the correct value. Otherwise, <code>params[:party][:hostess_id]</code> is <code>0</code> and <code>params[:party][:hostess]</code> has the data to create a new <code>Hostess</code> (e.g., <code>params[:party][:hostess][:first_name]</code>, etc.)</p>
  
  <p>In the <code>Parties</code> controller:</p>
  
  <pre><code>def create
    if params[:party][:hostess_id] == 0
      # create new hostess record
      if @hostess = Person.create!(params[:party][:hostess])
        params[:party][:hostess_id] = @hostess.id
      end
    end
    @party = Party.new(params[:party])
    if @party.save
      redirect_to @party, :notice =&gt; "Successfully created party."
    else
      @hostess = @party.build_hostess(params[:party][:hostess])
      render :action =&gt; 'new'
    end
  end
  </code></pre>
  
  <p>This is working fine when I pass in an existing <code>Hostess</code>, but it's not working when trying to create the new <code>Hostess</code> (fails to create the new <code>Hostess/Person</code> and thus fails on creating the new <code>Party</code>). Any suggestions?</p>