开发者

How do you set up a second database connection without the use of any yaml files?

开发者 https://www.devze.com 2023-02-11 22:28 出处:网络
I have an app running on Heroku and I want to set up a connection to a second database (from another app running on Heroku). All solutions I have seen for multiple DBs involve the database.yml file, b

I have an app running on Heroku and I want to set up a connection to a second database (from another app running on Heroku). All solutions I have seen for multiple DBs involve the database.yml file, but Heroku does not do things this way, they instructed me to use DATABASE_URL from one app in the other.

I think I need to do something like:

开发者_如何学C
DatabaseName::Base.establish_connection(DATABASE_URL)

and then I can use

establish_connection :DatabaseName

in the appropriate models. Where do I put

DatabaseName::Base.establish_connection(DATABASE_URL)

to have it available to all models? environment.rb? And what is the correct syntax for it?


Assuming you have two dbs DBOne and DBTwo which you want to connect (please fill up the data accordingly). Hope that 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
0

精彩评论

暂无评论...
验证码 换一张
取 消