Connecting to Multiple Databases | 117legacy:
adapter: mysql
database: my_db
username: user
password: pass
host: legacy_hostnew:
adapter: mysql
database: my_db
username: user
password: pass
host: new_hostThen, you can simply refer to these configuration blocks from the ActiveRecord class
definition using theActiveRecord::Base.establish_connection method:
class LegacyClient < ActiveRecord::Base
establish_connection "legacy"
endclass Client < ActiveRecord::Base
establish_connection "new"
endThis approach also works with multiple Rails environments. Just specify each envi-
ronment in thedatabase.yml file as usual:
legacy_development:
#...legacy_test:
#...legacy_production:
#...new_development:
#...new_test:
#...new_production:
#...Then, use theRAILS_ENV constant in the database configuration block name:
class LegacyClient < ActiveRecord::Base
establish_connection "legacy_#{RAILS_ENV}"
endclass Client < ActiveRecord::Base
establish_connection "new_#{RAILS_ENV}"
end