Created
November 7, 2011 11:19
-
-
Save BDQ/1344685 to your computer and use it in GitHub Desktop.
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
def connect_to_new(database) | |
klass_name = database.underscore.classify | |
# define class for each database so we get connection pool usage | |
unless (Module.const_get(klass_name) rescue false) | |
klass = Class.new(ActiveRecord::Base) | |
Object.const_set klass_name, klass | |
# only establish connection once | |
end | |
klass_name.constantize.send :establish_connection, multi_tenantify(database) | |
klass_name.constantize.send(:connection).send(:active?) # call active? to manually check if this connection is valid | |
rescue ActiveRecord::StatementInvalid => e | |
raise DatabaseNotFound, "The database #{environmentify(database)} cannot be found." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment