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.

Manish Singh
  • 72
  • 1
  • 4

1 Answers1

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

moritzg
  • 4,266
  • 3
  • 37
  • 62
Asif Meer
  • 71
  • 5