0

I am using apartment gem where it has multi tenancy. I want to have a solution like whenever that particular model is called or any execution happen it should switch to that specific database.

model name = RakeLog

main db_name = ABC

another db_name = ABC_logs

right now what I have done is created a class method in model

class RakeLog < ActiveRecord::Base

  def self.switch_to_log
    current = Apartment::Tenant.current
    Apartment::Tenant.switch!(current+'_logs')
  end
end

what I am doing now is calling this method everywhere when I need to switch to 'logs' db. I want something like whenever this model gets called it should switch to 'logs' db automatically. Any help is appreciated.

1

1 Answer 1

1

Your question is not clear, I think you want to connect to another database db_name ABC_logs from model class, if yes then this will help you.

class RakeLog < ActiveRecord::Base

  establish_connection ABC_logs_DB

  def self.switch_to_log
    current = Apartment::Tenant.current
    Apartment::Tenant.switch!(current+'_logs')
  end
end

You can check the complete tutorial here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.