title: Rails 3 - multiple level nesting - getting Uninitialized Constant Error
body: <p>I'm new to both programming and Ruby on Rails. I'm just trying with a sample 2 level deep nesting. When I followed Ryan's Scraps (<a href="http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes" rel="nofollow">http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes</a>) for 1 level deep nesting everything well and good but when I extended for 2 level deep I'm getting</p>
  
  <pre><code>NameError in ParentsController#new
  uninitialized constant Child::Grandchild
  </code></pre>
  
  <p>My models are like this</p>
  
  <pre><code>class Parent &lt; ActiveRecord::Base
    has_many :children
    accepts_nested_attributes_for :children, :allow_destroy =&gt; true
  end
  
  class Child &lt; ActiveRecord::Base
    belongs_to :parent
    has_many :grandchildren
    accepts_nested_attributes_for :grandchildren
  end
  
  class GrandChild &lt; ActiveRecord::Base
     belongs_to :child
  end
  </code></pre>
  
  <p>And my controller : new method for parent is -></p>
  
  <pre><code>def new
    @parent = Parent.new
    2.times do 
      child = @parent.children.build  
      2.times {child.grandchildren.build}
    end
  
  
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml =&gt; @parent }
    end
  end
  </code></pre>
  
  <p>Don't know what's the error , when I modified the models into </p>
  
  <pre><code>class Parent &lt; ActiveRecord::Base
    has_many :children, :through =&gt; :grandchildren
    has_many :grandchildren
    accepts_nested_attributes_for :children, :allow_destroy =&gt; true
    accepts_nested_attributes_for :grandchildren, :allow_destroy =&gt; true
  end
  
  class Child &lt; ActiveRecord::Base
    belongs_to :parent
    has_many :grandchildren
    accepts_nested_attributes_for :grandchildren
  end
  
  class GrandChild &lt; ActiveRecord::Base
    belongs_to :parent
    belongs_to :child
  end
  </code></pre>
  
  <p>then I'll get following error-- <code>uninitialized constant Parent::Grandchild</code>..</p>
  
  <p>I Don't know its a silly mistake or what... </p>
  
  <p>Thanks </p>
  
  <hr>
  
  <p>I have edited my question which is my real requirement. Instead of creating parent, children and grandchildren at a time as mentioned earlier I want to create parent first then child and grandchild together. I have edited the above code as mentioned below,</p>
  
  <p>My model:</p>
  
  <pre><code>class Parent &lt; ActiveRecord::Base
  has_many :children
  has_many :grand_children
  accepts_nested_attributes_for :children, :allow_destroy =&gt; true
  accepts_nested_attributes_for :grand_children, :allow_destroy =&gt; true
  end
  
  class Child &lt; ActiveRecord::Base
  belongs_to :parent
  has_many :grand_children
  accepts_nested_attributes_for :grand_children
  end
  
  class GrandChild &lt; ActiveRecord::Base
  belongs_to :parent
  belongs_to :child
  end
  </code></pre>
  
  <p>My children controller -new method:</p>
  
  <pre><code>    def new
  @parent = Parent.find(params[:parent_id])
  child = Child.new
  child.grand_children.build
  respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml =&gt; @child }
  end
  end
  </code></pre>
  
  <p>My children _form template is</p>
  
  <pre><code>&lt;%= form_for([@parent, @parent.children.build]) do |form|  %&gt;  
  &lt;div&gt;
  &lt;%= form.label :name %&gt;&lt;br /&gt;
  &lt;%= form.text_field :name %&gt;
  &lt;/div&gt;
  &lt;div class="field"&gt;
  &lt;%= form.label :sex %&gt;&lt;br /&gt;
  &lt;%= form.text_field :sex %&gt;
  &lt;/div&gt;
  &lt;div&gt;
  &lt;%= form.fields_for :grand_children do |grand_child_form| %&gt;
  &lt;%= render :partial =&gt; "grand_children/form", :locals =&gt; { :form =&gt; grand_child_form}     %&gt;
  &lt;% end %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Here I'm not getting any error as such but when I select new child the grand_child is not appearing, </p>
  
  <pre><code> &lt;%= form.fields_for :grand_children do |grand_child_form| %&gt;
    &lt;%= render :partial =&gt; "grand_children/form", :locals =&gt; { :form =&gt; grand_child_form} %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>is not getting reflected at all .</p>
  
  <p>Thanks in advance</p>