How do you set up a second database connection without the use of any yaml files?
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:
开发者_如何学CDatabaseName::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
精彩评论