How to set up a second database connection without using any yaml files?

I have an application running on Heroku, and I want to configure a connection to a second database (from another application running on Heroku). All the solutions that I saw for several databases are related to the database.yml file, but Heroku does not do it this way, they instructed me to use DATABASE_URL from one application to another.

I think I need to do something like:

 DatabaseName::Base.establish_connection(DATABASE_URL) 

and then i can use

 establish_connection :DatabaseName 

in the respective models. Where i put

 DatabaseName::Base.establish_connection(DATABASE_URL) 

so that it is available for all models? environment.rb? And why is this the correct syntax?

+4
source share
1 answer

Assuming you have two dbs DBOne and DBTwo that you want to connect (please fill in the data accordingly). Hope this helps.

  class DBOne < ActiveRecord::Base self.abstract_class = true establish_connection( :adapter => "mysql", :host => "hostname", :username => "myuser", :password => "mypass", :database => "database_one" ) end class ModelInDbOne <DBOne end class DBTwo < ActiveRecord::Base self.abstract_class = true establish_connection( :adapter => "mysql", :host => "hostname", :username => "myuser", :password => "mypass", :database => "database_two" ) end class ModelInDbTwo < DBTwo end 
+4
source

Source: https://habr.com/ru/post/1340192/


All Articles