title: Rails - one form to two models
body: <p>I am building a form in Rails (version 4.2.5) to collect basic information about stores, and I would like to have the form submit data to two separate models - one called Store, which collects information about the store, and one called Address, which just holds the address info.  I've seen this question floating around Stack and elsewhere, but I'm pretty new to Rails and could use a little more step-by-step guidance. (Also, I'm aware of the Rails geocoder gem, but don't want to use it in this project despite the fact that I am collecting address and lat/long info)</p>
  
  <p>For starters, here are my Store and Address models...</p>
  
  <pre><code>class Store &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :store_type
    has_one :address
  end
  
  class Address &lt; ActiveRecord::Base
    belongs_to :store
  end
  </code></pre>
  
  <p>and my StoresController...</p>
  
  <pre><code>class StoresController &lt; ApplicationController
  
      def new
          @store = Store.new
          @storeType = StoreType.all
      end
  
      def create
          @store = Store.new(store_params)
          @store.user = current_user
  
          if @store.save
              flash[:notice] = "New store created"
              redirect_to root_path
          else
              #ERROR STUFF
          end
      end
  
      private
      def store_params
          params.require(:store).permit(:user_id, :store_type_id, :latitude, :longitude, :name, :notes)
      end
  end
  </code></pre>
  
  <p>Finally, the form for creating a new store (not including address)...</p>
  
  <pre><code>&lt;%= form_for @store do |f| %&gt;
  
      &lt;%= f.hidden_field :latitude, :id =&gt; "latitude_field"  %&gt;
      &lt;%= f.hidden_field :longitude, :id =&gt; "longitude_field"  %&gt;
  
      &lt;div class="field"&gt;
          &lt;%= f.label :store_type_id %&gt;
          &lt;%= f.select :store_type_id, @storeType.map{ |type| [type.store_type.capitalize, type.id] } %&gt;
      &lt;/div&gt;
  
      &lt;div class="field"&gt;
          &lt;%= f.label :name %&gt;
          &lt;%= f.text_field :name  %&gt;
      &lt;/div&gt;
  
      &lt;div class="field"&gt;
          &lt;%= f.label :notes %&gt;
          &lt;%= f.text_field :notes  %&gt;
      &lt;/div&gt;
  
      &lt;div class="actions"&gt;
          &lt;%= f.submit :"Submit" %&gt;
      &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>So the finished form will pass the above fields to the Store model, and should also include fields for street, city, state, and zip, which are the fields that will be passed to the Address model.</p>
  
  <p>Overall, what is the best approach for this?</p>
  
  <p>And, as a follow-up question, my Address model has a column for store_id - will this column populate automatically with the id for the store as it is being created if both Store and Address models are being populated from the same form?  Or am I getting way out of my league here?</p>
  
  <p>Thank you.</p>