title: Categories stopped saving after creating a Join table in rails
body: <p>This is my first time creating a Join table and I am a bit stuck. I have two tables: Categories and Products. I originally had it set up where Products belong to on category and Categories had many products. I just created a join table and changed my associations to has_and_belongs_to_many. </p>
  
  <p>I have a form where multiple categories can be selected for a product but the categories selected are not saving in the database anymore. </p>
  
  <p>In my controller I have created an array for category_id in my product params and I am wondering if there is something in my database that needs to be updated as well? Should I change categroy_id in database?</p>
  
  <p>Here are my tables:</p>
  
  <pre><code>create_table "categories_products", id: false, force: :cascade do |t|
      t.integer "category_id", null: false
      t.integer "product_id",  null: false
    end
  
  create_table "categories", force: :cascade do |t|
      t.string   "name"
      t.text     "description"
      t.datetime "created_at",  null: false
      t.datetime "updated_at",  null: false
      t.integer  "product_id"
      t.integer  "category_id"
    end
  create_table "products", force: :cascade do |t|
      t.string   "title"
      t.text     "description"
      t.string   "image_url"
      t.integer  "price"
      t.datetime "created_at",                 null: false
      t.datetime "updated_at",                 null: false
      t.string   "image",       default: "{}"
      t.integer  "category_id"
      t.integer  "product_id"
    end
  </code></pre>
  
  <p>Here are my model associations: </p>
  
  <pre><code>class Product &lt; ActiveRecord::Base 
  has_and_belongs_to_many :categories, :join_table =&gt; :categories_products
  
  class Category &lt; ActiveRecord::Base  
  has_and_belongs_to_many :products, :join_table =&gt; :categories_products
  </code></pre>
  
  <p>the categories selector in my new product form:</p>
  
  <pre><code>&lt;div class="field"&gt;
      &lt;%= f.label :category_ids %&gt;&lt;br&gt;
  
      &lt;%= f.select :category_ids, Category.all.collect {|x| [x.name, x.id]}, {}, :multiple =&gt; true %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Products controller:</p>
  
  <pre><code>class ProductsController &lt; ApplicationController
    before_filter :authenticate_admin!, :except =&gt; [:index, :show, :earings]
    before_action :set_product, only: [:show, :edit, :update, :destroy]
  
    # GET /products
    # GET /products.json
  
     def index
      @products = Product.all
  
      respond_to do |format|
        format.html # index.html.erb
        format.js # index.js.erb
        format.json { render json: @products }
      end 
     end 
  
  
    def show
  
    end
  
  
    def new
      @products = Product.new
      @categories = Category.order(:name)
    end
    # GET /products/1/edit
    def edit
       @categories = Category.order(:name)
    end
  
    # POST /products
    # POST /products.json
    def  create
      @products = Product.new(product_params)
  
      respond_to do |format|
        if @product.save
          format.html { redirect_to  @product, notice: 'Product was successfully created.' }
          format.json { render :show, status: :created, location: @product }
        else
          format.html { render :new }
          format.json { render json: @product.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /products/1
    # PATCH/PUT /products/1.json
    def update
  
      respond_to do |format|
        if @product.update(product_params)
          format.html { redirect_to @product, notice: 'Product was successfully updated.' }
          format.json { render :show, status: :ok, location: @product }
        else
          format.html { render :edit }
          format.json { render json: @product.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /products/1
    # DELETE /products/1.json
    def destroy
      @product.destroy
      respond_to do |format|
        format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
        format.json { head :no_content }
       end
    end
  
  
   private
      # Use callbacks to share common setup or constraints between actions.
      def set_product
        @product = Product.find(params[:id])
  
      end
  
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def product_params
        params.require(:product).permit(:title, :description, :image, :price, :category_ids =&gt; [])
      end
  
  
  
      def search_params
        default_params = {}
        default_params.merge({user_id_eq: current_user.id}) if signed_in?
        # more logic here
        params[:q].merge(default_params)
      end  
  end
  </code></pre>