Search code examples
ruby-on-railsactiverecordacts-as-taggable-on

How to change connection to the database on acts as taggable on


I have used rails and gem(acts as taggable on). I want to change acts as taggable on's database setting. so,I set as below

-database.yml

default: &default
    adapter: mysql2
    database: my_db1
    host: localhost
    username: root
    password: pass

development:
    <<: *default

my_db2:
    <<: *default
    database: my_db2

-user_model.rb

class User < ActiveRecord::Base
    acts_as_taggable
    establish_connection :my_db2
end

and, execute as below

User.find(1)
//this line result is connection to my_db2


User.find(1).tag_list
//this line result is connection to my_db1
//i want to connect to my_db2 in this code(by acts as taggable on)

I don't know how to resolve this problem.

Thanks.


Solution

  • ok so you need to override the tags class, try creating a file called tags.rb inside your config/initializers folder and then add this

    module ActsAsTaggableOn
      class Tag
        establish_connection :my_db2
      end
    end
    

    Restart your server and then it should work.