title: HABTM relationships and accepts_nested_attributes_for
body: <p>I have a form that lets me create <em>new blog posts</em> and I'd like to be able to create <em>new categories</em> from the same form.</p>
  
  <p>I have a habtm relationship between posts and categories, which is why I'm having trouble with this.</p>
  
  <p>I have the following 2 models:</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    has_and_belongs_to_many :categories
    attr_accessible :title, :body, :category_ids
  
    accepts_nested_attributes_for :categories # should this be singular? 
  end
  
  class Category &lt; ActiveRecord::Base
    has_and_belongs_to_many :posts
    attr_accessible :name
  end
  </code></pre>
  
  <p>My form lets me pick from a bunch of existing categories or create a brand new one.  My form is as follows.</p>
  
  <pre><code># using simple_form gem
  .inputs
    = f.input :title
    = f.input :body
  
    # the line below lets me choose from existing categories
    = f.association :categories, :label =&gt; 'Filed Under'
  
    # I was hoping that the code below would let me create new categories
    = f.fields_for :category do |builder|
      = builder.label :content, "Name"
      = builder.text_field :content
  </code></pre>
  
  <p>When I submit my form, it gets processed but the new category is not created. My command prompt output tells me:</p>
  
  <pre><code>WARNING: Can't mass-assign protected attributes: category
  </code></pre>
  
  <p>But, if I add <code>attr_accessible :category</code>, I get a big fat crash with error message "unknown attribute: category".</p>
  
  <p>If I change the fields_for target to :categories (instead of category) then my form doesn't even display.</p>
  
  <p>I've spent a while trying to figure this out, and watched the recent railscasts on nested_models and simple_form but couldn't get my problem fixed.</p>
  
  <p>Would this be easier if I was using a has_many :through relationship (with a join model) instead of a habtm?</p>