Skip to content

Instantly share code, notes, and snippets.

@stefanluptak
Created May 19, 2010 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanluptak/406213 to your computer and use it in GitHub Desktop.
Save stefanluptak/406213 to your computer and use it in GitHub Desktop.
module UseMultiDB
def use_db(db_name, options={})
begin
establish_connection "#{db_name}_#{RAILS_ENV}"
set_table_name options[:table_name] unless options[:table_name].blank?
self.inheritance_column = '_sti_type_disabled' unless options[:sti]
rescue
Rails.logger.info "ERROR unable to establish connection to database '#{db_name}_#{RAILS_ENV}'"
end
end
end
ActiveRecord::Base.extend UseMultiDB
# USAGE
#
# class SomeClassInOtherDB < ActiveRecord::Base
# use_db :other_db, :table_name => :weird_name, :sti => false
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment