title: Nested forms in Rails using has_many :through
body: <p>I am having trouble figuring out how to make a nested form using a has_many :through relationship. I used this <a href="http://railscasts.com/episodes/196-nested-model-form-part-1" rel="nofollow">Railscast</a> and I took at look at <a href="http://iqbalfarabi.net/2011/01/20/rails-nested-form-with-has-many-through-association/" rel="nofollow">this tutorial</a> and lots of the questions on Stack Overflow and elsewhere around Google. </p>
  
  <p>I'm trying to make a way to create tags via the articles form. My code has gone through lots of iterations based on information from lots of different sources and none of them have worked, but right now I have </p>
  
  <p>A class for articles</p>
  
  <pre><code>class Article &lt; ActiveRecord::Base
      attr_accessible :content, :heading, :image, :tag_ids, :tags, :tag_name, :tag_attributes
  
    belongs_to :user
    has_many :comments, :dependent =&gt; :destroy
    has_many :article_tags
    has_many :tags, :through =&gt; :article_tags
    accepts_nested_attributes_for :tags, :reject_if =&gt; proc { |attributes| attributes['tag_name'].blank? }
  ...
  end
  </code></pre>
  
  <p>A class for tags</p>
  
  <pre><code>class Tag &lt; ActiveRecord::Base
    attr_accessible :tag_name
  
    has_many :article_tags
    has_many :articles, :through =&gt; :article_tags
  end
  </code></pre>
  
  <p>A class for article_tags</p>
  
  <pre><code>class ArticleTag &lt; ActiveRecord::Base
    belongs_to :article
    belongs_to :tag
  end
  </code></pre>
  
  <p>The New in my articles_controller.rb is like this:</p>
  
  <pre><code>def new
    @article = Article.new
    @tags = Tag.find(:all)
    article_tag = @article.article_tags.build()
    @article_tags = @article.tags.all
    @article.article_tags.build.build_tag
    3.times do
        article_tag = @article.article_tags.build()
    end
  end
  </code></pre>
  
  <p>And my form for articles is currently like this (I have gone back and forth between nesting the fields_for :tags inside the fields_for :article_tags or just letting them be on their own): </p>
  
  <pre><code>&lt;%= form_for @article , :html =&gt; { :multipart =&gt; true } do |f| %&gt;
   ...excerpted...
  &lt;%= f.fields_for :article_tags do |t| %&gt;
    &lt;%= t.fields_for :tags do |ta| %&gt;
      &lt;%= ta.label :tag_name, "Tag name" %&gt;
      &lt;%= ta.text_field :tag_name %&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I realize this is probably messy; I'm pretty new at this and I'm trying to figure it out. Do I have to add anything to the articles_controller create? Is it something to do with the attr_accessible? Or should I do something completely different?</p>
  
  <p>EDIT:</p>
  
  <p>Here are the request parameters after making the change suggested by Hck and creating a new article, selecting an existing tag with tag_id 3 and trying to also create a new tag at the same time:</p>
  
  <p>Started POST "/articles" for 127.0.0.1 at 2011-08-10 19:05:46 +1000
    Processing by ArticlesController#create as HTML
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"5CQuV4RWfFZD1uDjv1DrZbIe+GB/sDQ6yiAETZutmZ4=", "article"=>{"heading"=>"Test heading", "content"=>"Test Content", "tag_ids"=>["3"], "article_tags"=>{"tags"=>{"tag_name"=>"Test tag"}}}, "commit"=>"Submit"}
    User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  WARNING: Can't mass-assign protected attributes: article_tags
    Tag Load (0.4ms)  SELECT "tags".* FROM "tags" WHERE "tags"."id" = 3 LIMIT 1
    AREL (0.4ms)  INSERT INTO "articles" ("content", "user_id", "created_at", "updated_at", "heading", "image_file_name", "image_content_type", "image_file_size") VALUES ('Test Content', 1, '2011-08-10 09:05:46.228951', '2011-08-10 09:05:46.228951', 'Test heading', NULL, NULL, NULL)
    AREL (0.2ms)  INSERT INTO "article_tags" ("article_id", "tag_id", "created_at", "updated_at") VALUES (88, 3, '2011-08-10 09:05:46.243076', '2011-08-10 09:05:46.243076')
  [paperclip] Saving attachments.
  Redirected to [localhost]
  Completed 302 Found in 212ms</p>
  
  <p>And if I add :article_tags to the attr_accessible for Article and try again, I get:</p>
  
  <p>Started POST "/articles" for 127.0.0.1 at 2011-08-10 19:11:49 +1000
    Processing by ArticlesController#create as HTML
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"5CQuV4RWfFZD1uDjv1DrZbIe+GB/sDQ6yiAETZutmZ4=", "article"=>{"heading"=>"Test heading", "content"=>"Test content", "tag_ids"=>["3"], "article_tags"=>{"tags"=>{"tag_name"=>"Test tag "}}}, "commit"=>"Submit"}
    User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
    Tag Load (0.4ms)  SELECT "tags".* FROM "tags" WHERE "tags"."id" = 3 LIMIT 1
  Completed   in 119ms</p>
  
  <p>ActiveRecord::AssociationTypeMismatch (ArticleTag(#2165285820) expected, got Array(#2151973780)):
    app/controllers/articles_controller.rb:32:in `create'</p>