title: Rails 5 Seclect2 Acts_as_taggable Populates tag name on first product then sets the id as the name on all other products
body: <p>So I'm building a Rails 5 app using Select2-rails and acts_as_taggable gems. </p>
  
  <p>The selcet2 box is working fine and creates the tags as it should, but only when it's a new tag being made. </p>
  
  <p>Any tag selected after the initial tag is created, is duplicated but saved with the id as the tag name and a new id assigned to it. I have no idea why this is happening! </p>
  
  <p><strong>Initial Product Created with associated Tags:</strong>
  <a href="https://i.stack.imgur.com/oltq6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oltq6.png" alt="enter image description here"></a></p>
  
  <p><strong>2nd Product created but saves tag id's as the tag name and assigns new ID</strong>
  <a href="https://i.stack.imgur.com/Ez73q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ez73q.png" alt="enter image description here"></a></p>
  
  <p><strong>my Select2 Js:</strong></p>
  
  <pre><code>// JS Initializer
  $(document).on('turbolinks:load', function() {
    // Select2 Tag Creation
    $('.multiple-input').each(function() {
      $(this).select2({
        tags: true,
        tokenSeperators: [','],
        theme: 'bootstrap',
        placeholder: 'seperated by space',
        minimumInputLength: 3,
        ajax: {
          url: "/tags.json",
          dataType: 'json',
          processResults: function(data) {
            console.log(data)
            return {results: data};
          }
        }
      });
    });
  
  });
  </code></pre>
  
  <p><strong>My tags controller thats populating the /tags.json</strong></p>
  
  <pre><code>class TagsController &lt; ApplicationController
  
    def index
      tags = ActsAsTaggableOn::Tag
        .where("name ILIKE ?", "%#{params[:term]}%")
        .select("id, name as text")
  
      respond_to do |format|
        format.json {render json: tags}
      end
    end
  
  end
  </code></pre>
  
  <p><strong>Stack Trace where Tags are created properly:</strong></p>
  
  <pre><code>Started POST "/products" for ::1 at 2019-04-21 11:34:18 -0600
  Processing by ProductsController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"SPc9Ri+RK6yIm19Six5EGhUBPez/IyV7rccKlBcUcR16pNzAFCagEfEg6YelehK2Bk/wlzC54ziWayJyHDqjdQ==", "product"=&gt;{"name"=&gt;"T-Shirt", "description"=&gt;"Test Shirt", "price"=&gt;"19.99", "tag_list"=&gt;["", "T-Shirt", "Mens", "Womens"]}, "commit"=&gt;"Create Product"}
     (0.3ms)  BEGIN
    ↳ app/controllers/products_controller.rb:39
    Product Create (1.2ms)  INSERT INTO "products" ("name", "description", "price", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["name", "T-Shirt"], ["description", "Test Shirt"], ["price", "19.99"], ["created_at", "2019-04-21 17:34:18.330742"], ["updated_at", "2019-04-21 17:34:18.330742"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.7ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('t-shirt') OR LOWER(name) = LOWER('mens') OR LOWER(name) = LOWER('womens'))
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Exists (0.4ms)  SELECT  1 AS one FROM "tags" WHERE "tags"."name" = $1 LIMIT $2  [["name", "T-Shirt"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Create (0.6ms)  INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id"  [["name", "T-Shirt"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('t-shirt') OR LOWER(name) = LOWER('mens') OR LOWER(name) = LOWER('womens'))
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Exists (0.5ms)  SELECT  1 AS one FROM "tags" WHERE "tags"."name" = $1 LIMIT $2  [["name", "Mens"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Create (0.3ms)  INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id"  [["name", "Mens"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('t-shirt') OR LOWER(name) = LOWER('mens') OR LOWER(name) = LOWER('womens'))
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Exists (0.2ms)  SELECT  1 AS one FROM "tags" WHERE "tags"."name" = $1 LIMIT $2  [["name", "Womens"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Create (0.2ms)  INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id"  [["name", "Womens"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (2.0ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 1], ["taggable_type", "Product"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.2ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Exists (0.5ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."taggable_id" = $3 AND "taggings"."context" = $4 AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT $5  [["tag_id", 1], ["taggable_type", "Product"], ["taggable_id", 1], ["context", "tags"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Create (1.3ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["tag_id", 1], ["taggable_type", "Product"], ["taggable_id", 1], ["context", "tags"], ["created_at", "2019-04-21 17:34:18.422464"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Update All (0.4ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = $1  [["id", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.2ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Exists (0.5ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."taggable_id" = $3 AND "taggings"."context" = $4 AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT $5  [["tag_id", 2], ["taggable_type", "Product"], ["taggable_id", 1], ["context", "tags"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Create (0.3ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["tag_id", 2], ["taggable_type", "Product"], ["taggable_id", 1], ["context", "tags"], ["created_at", "2019-04-21 17:34:18.428044"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Update All (0.3ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = $1  [["id", 2]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.2ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Exists (0.4ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."taggable_id" = $3 AND "taggings"."context" = $4 AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT $5  [["tag_id", 3], ["taggable_type", "Product"], ["taggable_id", 1], ["context", "tags"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Create (0.3ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["tag_id", 3], ["taggable_type", "Product"], ["taggable_id", 1], ["context", "tags"], ["created_at", "2019-04-21 17:34:18.432148"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Update All (0.3ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = $1  [["id", 3]]
    ↳ app/controllers/products_controller.rb:39
     (40.8ms)  COMMIT
    ↳ app/controllers/products_controller.rb:39
  Redirected to http://localhost:3000/products/1
  Completed 302 Found in 149ms (ActiveRecord: 53.2ms)
  </code></pre>
  
  <p><strong>Stack Trace where Tags are created improperly</strong></p>
  
  <pre><code>Started POST "/products" for ::1 at 2019-04-21 11:36:37 -0600
  Processing by ProductsController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"ec/uY1MZ4g4dQmpzp2Uu03VgvRJrjDQB7e/H3ohAjstLnA/laK5ps2T53KaJAXh/Zi5waaQW8kLWQ+84g25cow==", "product"=&gt;{"name"=&gt;"TEST 2", "description"=&gt;"another test with messed up tags", "price"=&gt;"19.99", "tag_list"=&gt;["", "1", "2", "3"]}, "commit"=&gt;"Create Product"}
     (0.1ms)  BEGIN
    ↳ app/controllers/products_controller.rb:39
    Product Create (0.4ms)  INSERT INTO "products" ("name", "description", "price", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["name", "TEST 2"], ["description", "another test with messed up tags"], ["price", "19.99"], ["created_at", "2019-04-21 17:36:37.941777"], ["updated_at", "2019-04-21 17:36:37.941777"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.6ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('1') OR LOWER(name) = LOWER('2') OR LOWER(name) = LOWER('3'))
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Exists (0.5ms)  SELECT  1 AS one FROM "tags" WHERE "tags"."name" = $1 LIMIT $2  [["name", "1"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Create (0.6ms)  INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id"  [["name", "1"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.4ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('1') OR LOWER(name) = LOWER('2') OR LOWER(name) = LOWER('3'))
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Exists (0.3ms)  SELECT  1 AS one FROM "tags" WHERE "tags"."name" = $1 LIMIT $2  [["name", "2"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Create (0.3ms)  INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id"  [["name", "2"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('1') OR LOWER(name) = LOWER('2') OR LOWER(name) = LOWER('3'))
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Exists (0.2ms)  SELECT  1 AS one FROM "tags" WHERE "tags"."name" = $1 LIMIT $2  [["name", "3"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Create (0.2ms)  INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id"  [["name", "3"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.6ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 2], ["taggable_type", "Product"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.2ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Exists (0.4ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."taggable_id" = $3 AND "taggings"."context" = $4 AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT $5  [["tag_id", 4], ["taggable_type", "Product"], ["taggable_id", 2], ["context", "tags"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Create (0.4ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["tag_id", 4], ["taggable_type", "Product"], ["taggable_id", 2], ["context", "tags"], ["created_at", "2019-04-21 17:36:37.955779"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Update All (0.3ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = $1  [["id", 4]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.2ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Exists (0.7ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."taggable_id" = $3 AND "taggings"."context" = $4 AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT $5  [["tag_id", 5], ["taggable_type", "Product"], ["taggable_id", 2], ["context", "tags"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Create (0.4ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["tag_id", 5], ["taggable_type", "Product"], ["taggable_id", 2], ["context", "tags"], ["created_at", "2019-04-21 17:36:37.960667"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Update All (0.4ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = $1  [["id", 5]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Load (0.5ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Exists (0.5ms)  SELECT  1 AS one FROM "taggings" WHERE "taggings"."tag_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."taggable_id" = $3 AND "taggings"."context" = $4 AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL LIMIT $5  [["tag_id", 6], ["taggable_type", "Product"], ["taggable_id", 2], ["context", "tags"], ["LIMIT", 1]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tagging Create (0.3ms)  INSERT INTO "taggings" ("tag_id", "taggable_type", "taggable_id", "context", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["tag_id", 6], ["taggable_type", "Product"], ["taggable_id", 2], ["context", "tags"], ["created_at", "2019-04-21 17:36:37.966087"]]
    ↳ app/controllers/products_controller.rb:39
    ActsAsTaggableOn::Tag Update All (0.3ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = $1  [["id", 6]]
    ↳ app/controllers/products_controller.rb:39
     (0.8ms)  COMMIT
    ↳ app/controllers/products_controller.rb:39
  Redirected to http://localhost:3000/products/2
  Completed 302 Found in 30ms (ActiveRecord: 9.9ms)
  </code></pre>
  
  <p><strong>Products Controller</strong></p>
  
  <pre><code>class ProductsController &lt; ApplicationController
    before_action :set_product, only: [:show, :edit, :update, :destroy]
  
    def index
      @products = Product.all
  
      respond_to do |format|
        format.html
        format.json { render json: @products }
      end
    end
  
    def show
  
      respond_to do |format|
        format.html
        format.json { render json: @product }
      end
    end
  
    def new
      @product = Product.new
  
      respond_to do |format|
        format.html
        format.json { render json: @product }
      end
    end
  
    def edit
    end
  
    def create
      @product = Product.new(product_params)
  
      respond_to do |format|
        if @product.valid?
  
          @product.save
          format.html { redirect_to @product, flash: { success: "#{@product.name.titleize} has been created &amp; added to the store." } }
          format.json { render json: @product, status: :created, location: @project }
        else
          format.html { render action: "new" }
          format.json { render json: @product.errors, status: :unprocessable_entity }
        end
      end
    end
  
    def update
    end
  
    def destroy
    end
  
    def tagged
      if params[:tag].present?
        @products = Product.tagged_with(params[:tag])
      else
        @products = Product.all
      end
    end
  
    private
  
      def set_product
        @product = Product.find(params[:id])
      end
  
      def product_params
        params.require(:product).permit(:name, :description, :price, :image, :tag_list, tag_list: [])
      end
  
  end
  </code></pre>