Created
June 12, 2013 11:32
-
-
Save jbonney/5764538 to your computer and use it in GitHub Desktop.
Manage multiple databases in Rails. Connect to different DB based on the model.
http://stackoverflow.com/questions/6122508/connecting-rails-3-1-with-multiple-databases, http://ilikestuffblog.com/2012/09/21/establishing-a-connection-to-a-non-default-database-in-rails-3-2-2/, https://www.linkedin.com/groups/Multiple-Databases-Rails-31-120725.S.554…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| db1_production: | |
| adapter: mysql2 | |
| encoding: utf8 | |
| database: db1 | |
| username: user_db1 | |
| password: mypass | |
| host: localhost | |
| timeout: 5000 | |
| db2_production: | |
| adapter: mysql2 | |
| encoding: utf8 | |
| database: db2 | |
| username: user_db2 | |
| password: mypass | |
| host: other_host | |
| timeout: 5000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module DatabaseConnections | |
| class Db1 < ActiveRecord::Base | |
| self.abstract_class = true | |
| establish_connection configurations["db1_#{Rails.env}"] | |
| end | |
| class Db2 < ActiveRecord::Base | |
| self.abstract_class = true | |
| establish_connection configurations["db2_#{Rails.env}"] | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ModelStoredInDb1 < DatabaseConnections::Db1 | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ModelStoredInDb2 < DatabaseConnections::Db2 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment