title: Rails 3 Nested Model Form
body: <p>I am having some issues with nested models in a form, using Rails 3.1rc4.</p>
  
  <p>I presently have models that look like this:</p>
  
  <pre><code>class Sale &lt; ActiveRecord::Base
    attr_accessible :customer_id, :vehicle_id, :sale_date
  
    belongs_to :customer
    accepts_nested_attributes_for :customer
  end
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Customer &lt; ActiveRecord::Base
    attr_accessible :dealership_id, :first_name, :last_name, :address1, :email
  
    belongs_to :dealership
  
    has_many :sales
    has_many :vehicles, :through =&gt; :sales
  
  end
  </code></pre>
  
  <p>I've obviously truncated these slightly, but all the important info is there.</p>
  
  <p>I am attempting to set up a sale form that will also allow me to create a new customer, hence the <code>accepts_nested_attributes_for :customer</code> line in the sale model.</p>
  
  <p>My form view looks like (again truncated, only the important part):</p>
  
  <pre><code>    &lt;%= form_for @sale, :html =&gt; {:class =&gt; 'fullform'} do |f| %&gt;
  
          &lt;%= f.error_messages %&gt;
  
          &lt;%= field_set_tag 'Customer Details' do %&gt;
              &lt;% f.fields_for :customer do |builder| %&gt;
                  &lt;%= builder.label :first_name %&gt;&lt;br&gt;
                  &lt;%= builder.text_field :first_name %&gt;
              &lt;% end %&gt;
          &lt;% end %&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p>The problem I am having is that neither the text field nor the label for :first_name are showing up when the form is rendered - there is no error message, it just doesn't appear.</p>
  
  <p>I should mention that I have tried both with and without <code>@sale.customer.build</code> in the <code>new</code> method of my controller, but it seems to have had no effect.</p>
  
  <p>Thanks!</p>
  
  <p>Can anyone suggest what I am doing wrong?</p>
  
  <p>EDIT: For the avoidance of doubt, my sales controller's new method looks like:</p>
  
  <pre><code>def new
    @sale = Sale.new
    @sale.customer.build
  end
  </code></pre>