title: saving to multiple models from one form with rails
body: <p>i have been getting into rails over the course of the last couple weeks and i've run across a problem that I can not find the answer to. i feel like this is probably something really simple but it's driving me crazy so I am hoping to get a little bit a guidance...</p>
  
  <p>I am inserting data from 1 form into 2 models. one model is called Venue and one model is called Events. It is a one to many association where multiple events belong to venues and venues have many events. </p>
  
  <p>models:</p>
  
  <pre><code>class Venue &lt; ActiveRecord::Base
    has_many :events
    accepts_nested_attributes_for :events
  end
  
  class Event &lt; ActiveRecord::Base
    belongs_to :venue
  
    scope :upcoming, where('date &gt;= ?', Date.today)
   end  
  </code></pre>
  
  <p>controller:</p>
  
  <pre><code>class EventsController &lt; ApplicationController
  
  def new
        @venue = Venue.new
  end
  
  def create
  
  @venue = Venue.new(params[:venue])
  
      if @venue.save
          render :inline =&gt; "Success"
      else
          render('new')
      end
  end
  
  end
  </code></pre>
  
  <p>form:</p>
  
  <pre><code>&lt;%= form_for(@venue, :url =&gt;{:action =&gt; 'create'}) do |f| %&gt;
  
          Artist ID&lt;br /&gt;
          &lt;%= fields_for :event do |event_fields| %&gt;
              &lt;%= event_fields.text_field :artist_id %&gt;&lt;br/&gt;&lt;br /&gt;
          &lt;% end %&gt;
  
          Venue City&lt;br /&gt;
          &lt;%= f.text_field(:city) %&gt; &lt;br /&gt;&lt;br /&gt;
          Venue Name&lt;br /&gt;
          &lt;%= f.text_field(:name) %&gt;&lt;br/&gt;&lt;br/&gt;
  
  
          &lt;div class="actions"&gt;
              &lt;%= submit_tag "Save", :class =&gt; "btn primary" %&gt;
          &lt;/div&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>log output:</p>
  
  <pre><code>Started POST "/events" for 127.0.0.1 at 2012-02-17 19:57:24 -0500
  Processing by EventsController#create as HTML
  Parameters: {"utf8"=&gt;"✓",   "authenticity_token"=&gt;"p3llx5KsYn6gyjP9g2qwzXr+0rjh3h/o34h/iqvqjRo=", "event"=&gt;{"artist_id"=&gt;"124"}, "venue"=&gt;{"city"=&gt;" Boston", "name"=&gt;"Bostons Fa'v"}, "commit"=&gt;"Save"}
  (0.2ms)  BEGIN
  SQL (0.8ms)  INSERT INTO "venues" ("address_1", "address_2", "capacity", "city", "country", "created_at", "created_by", "name", "state", "updated_at", "url", "zip") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["address_1", nil], ["address_2", nil], ["capacity", nil], ["city", " Boston"], ["country", nil], ["created_at", Sat, 18 Feb 2012 00:57:24 UTC +00:00], ["created_by", nil], ["name", "Bostons Fa'v"], ["state", nil], ["updated_at", Sat, 18 Feb 2012 00:57:24 UTC +00:00], ["url", nil], ["zip", nil]]
  (0.4ms)  COMMIT
  Rendered inline template (0.2ms)
  Completed 200 OK in 5ms (Views: 0.6ms | ActiveRecord: 1.4ms)
  </code></pre>
  
  <p>As you can see its not failing but it is only submitting to the 1 model. My understand is that by adding "accepts_nested_attributes_for :events" the Venue model knows to then go to my Events table, create a new event with the event data I provide (in this example just artist_id), and then automatically insert the id field from my venue table into venue_id.</p>
  
  <p>The more I think about this here the more I think I might be missing a step and....If anyone has a second I would appreciate hearing your thought process on this.</p>
  
  <p>Thanks </p>