Skip to content

Instantly share code, notes, and snippets.

@BDQ
Created November 7, 2011 11:19
Show Gist options
  • Save BDQ/1344685 to your computer and use it in GitHub Desktop.
Save BDQ/1344685 to your computer and use it in GitHub Desktop.
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