Dynamicaly create DB connection

It is very specific project and I realized that. I have more than one databases in my project. First is a core DB and it is used always. Authorized data (username, password) and configuration (auth data to another DB)
I’m trying figured out how to get credentials to external database from primary database, then pass it to ActiveRecord::getDb() method of model which should use only external DB

Probably I found solution.

Definetly this solve my issue.