title: RAILS: Nil class for belongs_to
body: <p>I want to make nested form. I have:</p>
  
  <p><strong>advert.rb</strong></p>
  
  <pre><code>has_many :photos
  belongs_to :car
  
  accepts_nested_attributes_for :photos
  accepts_nested_attributes_for :car
  </code></pre>
  
  <p><strong>controller.rb</strong></p>
  
  <pre><code>def new
      @advert = Advert.new
      @advert.photos.new # OK
      @advert.car.new    # ERROR
  end
  </code></pre>
  
  <p>So for <code>@advert.photos.new</code> everything is ok (form generating <code>advert[photos_attributes][o][image]</code>) but for <code>@advert.car.new</code> i'm gettings error: <code>undefined method 'new' for nil:NilClass</code></p>
  
  <p><strong>UPD 1:</strong>
  After reading tonns of articles about nested_attributes, i understood that we can't use <code>accepts_nested_attributes_for</code> for parent in children (<code>belongs_to</code>) model. </p>
  
  <p>So, what is the best way to achieve my goal? Make complex form from example i provided in this question?</p>