title: One form to update multiple models
body: <p>I'm trying to build a form that allows a user to create a new Post, a Tag for that post, and a TagType for that tag, all with one submit button. </p>
  
  <p>My models are set up as follows:</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
  belongs_to :user
  has_many :post_tag_relationships, dependent: :destroy
  has_many :tags, through: :post_tag_relationships
  .
  .
  end
  
  class Tag &lt; ActiveRecord::Base
  has_many :reverse_post_tag_relationships, class_name: "PostTagRelationship"
  has_many :posts, through: :reverse_post_tag_relationships, 
                      class_name: "PostTagRelationship"
  belongs_to :tag_type
  .
  .
  end
  
  class TagType &lt; ActiveRecord::Base
  has_many :tags
  .
  .
  end
  </code></pre>
  
  <p>In the page controller where the form is located, I have the methods defined as follows:</p>
  
  <pre><code>@post = current_user.posts.build
  @tag = @post.tags.build
  @tag_type = @tag.tag_type.build
  </code></pre>
  
  <p>My form displays just fine if I only include the post and tag methods, as in:</p>
  
  <pre><code>&lt;%= form_for(@post) do |f| %&gt;
  &lt;%= render 'shared/error_messages', object: f.object %&gt;
  &lt;div class="field"&gt;
  &lt;%= f.text_area :content, placeholder: "Compose new post..." %&gt;
  &lt;/div&gt;
  
  &lt;%= fields_for(@tag) do |u| %&gt;
  &lt;div class="field"&gt;
  &lt;%= u.text_area :name, placeholder: "Tag" %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;%= f.submit "Post", class: "btn btn-large btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>But when I add fields_for(@tag_type), with:</p>
  
  <pre><code>&lt;%= fields_for(@tag_type) do |y| %&gt;
  &lt;div class="field"&gt;
  &lt;%= y.select :name %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I get an undefined method 'model name' for NilClass:Class</p>
  
  <p>I'm pretty new with Rails, and my guess is that it has something to do with the fact that a tag belongs_to tag_types (whereas a post has_many tags). If anyone knows a fix, it would be much appreciated. Thanks.</p>