title: Rails HABTM checkboxes don't save to database
body: <p>I am beginning Rails programming and all I want to accomplish is to have a list of check boxes from which I can choose the corresponding categories for a product.
  I have followed the railscast on this matter, but it doesn't seem to work for me.</p>
  
  <p>I work with Rails 3.0.9 and Ruby 1.8.7.</p>
  
  <p>I have the models properly set up and the migration for the join table as well.
  When I try to edit a product, the category_ids get POSTed, but they are never saved to the database.</p>
  
  <p>Log :</p>
  
  <pre><code>Started POST "/products/1" for 127.0.0.1 at Mon Sep 19 01:39:33 +0300 2011
    Processing by ProductsController#update as HTML
    Parameters: {"commit"=&gt;"Update Product",     "authenticity_token"=&gt;"x2Z5GGiNh9yLz6xjuQaMqdGW3Gw7dYe8dSghyrFpiYk=", "utf8"=&gt;"���", "id"=&gt;"1", "product"=&gt;{"name"=&gt;"Magicianulx2", "price"=&gt;"121", "category_ids"=&gt;["1"]}}
    Product Load (0.1ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 1 LIMIT 1
  WARNING: Can't mass-assign protected attributes: category_ids
  Redirected to http://localhost:3000/products/1
  Completed 302 Found in 10ms
  
  
  Started GET "/products/1" for 127.0.0.1 at Mon Sep 19 01:39:33 +0300 2011
    Processing by ProductsController#show as HTML
    Parameters: {"id"=&gt;"1"}
    Product Load (0.1ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 1 LIMIT 1
  Rendered products/show.html.erb within layouts/application (4.2ms)
  Completed 200 OK in 11ms (Views: 6.1ms | ActiveRecord: 0.2ms)
  </code></pre>
  
  <p>Models :</p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
    attr_accessible :name, :price
    has_and_belongs_to_many :categories
  end
  
  class Category &lt; ActiveRecord::Base
    attr_accessible :name
    has_and_belongs_to_many :products
  end
  </code></pre>
  
  <p>Migration :</p>
  
  <pre><code>class AddCategoriesProducts &lt; ActiveRecord::Migration
    def self.up
      create_table :categories_products, :id =&gt; false do |t|
        t.integer :category_id
        t.integer :product_id
      end
    end
  
    def self.down
      drop_table :categories_products
    end
  end
  </code></pre>
  
  <p>Controller (update method in product controller)</p>
  
  <pre><code>  def update
      @product = Product.find(params[:id])
      if @product.update_attributes(params[:product])
        redirect_to @product, :notice  =&gt; "Successfully updated product."
      else
        render :action =&gt; 'edit'
      end
    end
  </code></pre>
  
  <p>My _form.html.erb in views/products</p>
  
  <pre><code>&lt;%= form_for @product do |f| %&gt;
    &lt;%= f.error_messages %&gt;
    &lt;p&gt;
      &lt;%= f.label :name %&gt;&lt;br /&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :price %&gt;&lt;br /&gt;
      &lt;%= f.text_field :price %&gt;
    &lt;/p&gt;
  
  &lt;% for category in Category.find(:all) %&gt;
          &lt;div&gt;
                  &lt;%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %&gt;
                  &lt;%= category.name %&gt;
          &lt;/div&gt;
     &lt;% end %&gt;
  
  &lt;p&gt;&lt;%= f.submit %&gt;&lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>The controllers and the views were all generated using nifty scaffolding.
  Everything else seems to work just fine, but the values from the check boxes don't get saved to the database.
  Adding them by hand via the rails console works just fine.
  Any ideas on what I am doing wrong?</p>