I am developing a multi-company website in which each company has its database and when the user logs in, he will then access the database of the company he is associated with, each user having a record called "empresa .id. " The problem comes when a user of an "X" company starts a session and then another user of a company "Y" also logs in, making it possible to change the database for the whole system, thanks to this last user of the company "Y" also for the user of the company "X" and all other users instead of each user making use of a specific database without being affected by the database of other users.
This is my code to change the database (I use the in applicationController):
before_action :set_database
def set_database
if usuario_signed_in?
empresa = (current_usuario.empresa_id).to_s.to_sym
ActiveRecord::Base.establish_connection(empresa)
else
ActiveRecord::Base.establish_connection(:'232')
end
end
The variable "company" will take the value of "company_id" of the user and make the connection with the database of the company, for example "company" has a value "32" then it will make the connection in database.yml with :
'32':
adapter: sqlserver
mode: dblib
dataserver:
host: host
port: 1433
database: database
username: user
password: pass
timeout: 60000
azure: true
How can I log in with several users without affecting others who have a different database?