Created
April 22, 2015 23:27
-
-
Save mmasashi/3f69ce0c199613b90119 to your computer and use it in GitHub Desktop.
Load Rails submodule models with different database.
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
# rails_root/config/initializers/load_submodule_models.rb | |
RAILS_MODEL_LIST = %w( | |
Account | |
Plan | |
User | |
UserPreference | |
) | |
def apply_db_change(class_name) | |
eval(<<EOT) | |
class #{class_name} < ActiveRecord::Base | |
establish_connection( | |
:adapter => "mysql2", | |
:host => "localhost", | |
:database => "another_db_name", | |
:username => "root", | |
:password => nil | |
) | |
end | |
EOT | |
end | |
RAILS_MODEL_LIST.each do |class_name| | |
apply_db_change(class_name) | |
require class_name.underscore # load model forcibly | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment