Skip to content

Instantly share code, notes, and snippets.

@mmasashi
Created April 22, 2015 23:27
Show Gist options
  • Save mmasashi/3f69ce0c199613b90119 to your computer and use it in GitHub Desktop.
Save mmasashi/3f69ce0c199613b90119 to your computer and use it in GitHub Desktop.
Load Rails submodule models with different database.
# 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