I'm using a Rails application to access a Dynamics NAV Database (today is called Dynamics 365 Business Central). In order to make it multi tenant, they partition the DB using table name prefixes ex: Company 1$Warehouse Entry
or Company 2$Warehouse Entry
.
I wanted to use a field defined in my User
model in order to set the company name that it access.
In order to access the current user in the model I have used the following on application_controller.rb
before_action -> { User.current = current_user }
And then I set a base class where it declares the following instance method:
def self.set_company_name
company = User.current.nav_company ? User.current.nav_company + "$" : "Company 1$"
self.table_name = company + self.table_name
end
I also tried to use instance variables @@company
but after reading I saw that table names in rails are cached.
What I have done works, but only if you restart the application. The table name doesn't change in runtime.
How can I make it dynamic? I saw this Question but the answer didn't show how to do this.