title: has_and_belongs_to_many association in rails 4
body: <p>I've done everything as in here <a href="http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association" rel="nofollow">http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association</a>, but it doesn't work. I found some answers in stackoverflow, but still it doesn't work. I've made association for posters and types. But when I specify the type of the poster nothing happens. There is no adding to DB, and no types in the  Poster's view. Here is my PostrsTypes creating table: </p>
  
  <pre><code>class PostersTypes &lt; ActiveRecord::Migration
    def change
      create_table :posters_types, id: false do |t|
        t.integer :poster_id
        t.integer :type_id
      end
      add_index :posters_types, [:poster_id, :type_id]
    end
  end
  </code></pre>
  
  <p>Here is Poster model:</p>
  
  <pre><code>class Poster &lt; ActiveRecord::Base
    has_and_belongs_to_many :types
    belongs_to :user
  
    validates :title, :body, :publish_date, :user_id, :presence =&gt; true
  end
  </code></pre>
  
  <p>And Type model: </p>
  
  <pre><code>class Type &lt; ActiveRecord::Base
    has_and_belongs_to_many :posters
  
    validates :name, :presence =&gt; true
  end
  </code></pre>
  
  <p>Any ideas why nothing is writing in the DB?</p>
  
  <p>EDIT. That's my posters_controller, if you need it: </p>
  
  <pre><code>class PostersController &lt; ApplicationController
    before_action :set_poster, only: [:show, :edit, :update, :destroy]
    before_filter :authenticate_user!, :except =&gt; :index
    # GET /posters
    # GET /posters.json
    def index
      @posters = Poster.all
    end
  
    # GET /posters/1
    # GET /posters/1.json
    def show
    end
  
    # GET /posters/new
    def new
      @poster = Poster.new
    end
  
    # GET /posters/1/edit
    def edit
    end
  
    # POST /posters
    # POST /posters.json
   def create
      @poster = Poster.new(poster_params)
      @poster.user_id = current_user.id
      respond_to do |format|
        if @poster.save
          format.html { redirect_to @poster, notice: 'Poster was successfully created.' }
          format.json { render action: 'show', status: :created, location: @poster }
        else
          format.html { render action: 'new' }
          format.json { render json: @poster.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /posters/1
    # PATCH/PUT /posters/1.json
    def update
      respond_to do |format|
       if @poster.update(poster_params)
          format.html { redirect_to @poster, notice: 'Poster was successfully updated.' }
          format.json { head :no_content }
       else
          format.html { render action: 'edit' }
          format.json { render json: @poster.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /posters/1
    # DELETE /posters/1.json
    def destroy
      @poster.destroy
      respond_to do |format|
        format.html { redirect_to posters_url }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_poster
        @poster = Poster.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def poster_params
        params.require(:poster).permit(:title, :body, :publish_date, :type_ids)
      end
  end
  </code></pre>