ruby-on-railsacts-as-taggable-on

Rails 5 ActsAsTaggable Mass Update tags in rails console?


So I'm trying to update any of my relevant resources with a new tag, based off what the old one is.

Any Post that is tagged with test, I want to update it so that all those Posts that are tagged with test will now be tagged with new-testing-tag for example.

My other question is, if I have multiple tags associated with a Post, and I just wanted to update one of them, would the process by the same?

for example: Post has tags "javascript" and "premium" -- i just want to change "premium" to free. Is this possible?

tried all of these, in the rails console

Post.tagged_with("test").update_all(tag: "new-testing-tag")
Post.tagged_with("test").update_all(tags: "test2")
Post.tagged_with("test").update_all(tag_list: "test2")

none of them worked though, all of them throw this back: ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "tag" of relation "multiple_choice_questions" does not exist LINE 1: UPDATE "multiple_choice_questions" SET "tag" = 'test2' WHERE...

This is in my schema.db file

  create_table "taggings", force: :cascade do |t|
    t.bigint "tag_id"
    t.string "taggable_type"
    t.bigint "taggable_id"
    t.string "tagger_type"
    t.bigint "tagger_id"
    t.string "context", limit: 128
    t.datetime "created_at"
    t.index ["context"], name: "index_taggings_on_context"
    t.index ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
    t.index ["tag_id"], name: "index_taggings_on_tag_id"
    t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
    t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy"
    t.index ["taggable_id"], name: "index_taggings_on_taggable_id"
    t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable_type_and_taggable_id"
    t.index ["taggable_type"], name: "index_taggings_on_taggable_type"
    t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type"
    t.index ["tagger_id"], name: "index_taggings_on_tagger_id"
    t.index ["tagger_type", "tagger_id"], name: "index_taggings_on_tagger_type_and_tagger_id"
  end

  create_table "tags", force: :cascade do |t|
    t.string "name"
    t.integer "taggings_count", default: 0
    t.index ["name"], name: "index_tags_on_name", unique: true
  end

Solution

  • Okay, lets finish this task:

    I really couldn't find better solution for updating tag inside this gem ActsAsTaggableOn, but as I suppose before in my comment above we can manage to get this done by using rails migration and updating tags name with plain SQL.

    So, I did it like this: 1 - rails g migration UpdateTags (in terminal I run this command to generate new migration file) 2 - opened generated migration and wrote the code:

        class UpdateTags < ActiveRecord::Migration[5.2]
         def change
          execute "UPDATE tags SET name='somenewtag' WHERE LOWER('tags'.'name') LIKE 'rubyonrails.ba' ESCAPE '!';" 
         end
        end
    

    3 - save file and run rails db:migrate

    As you can see in sql command my goal was to update any tags with name value 'rubyonrails.ba' to new tags name with value 'somenewtag'. You can change this values by your needs. I hope this solution is good enough.

    Note: you can write many lines in one migration to update many tags in one migration.

    enter image description here