Skip to content

Instantly share code, notes, and snippets.

@mikechau
Created May 3, 2013 16:41
Show Gist options
  • Save mikechau/5510951 to your computer and use it in GitHub Desktop.
Save mikechau/5510951 to your computer and use it in GitHub Desktop.
Having Active Record Interface with 2 Databases
#In rails this is located in the config folder
# if you have the db defined in database.yml, you can set the establish_connection to establish_connection("db2")
development:
adapter: mysql2
database: indicator_development
pool: 5
username: root
password: root
socket: /var/run/mysqld/mysqld.sock
db2:
adapter: mysql2
database: db2
pool: 5
username: root
password: root
socket: /var/run/mysqld/mysqld.sock
# Located in models folder
Class Db2Base < ActiveRecord::Base
self.abstract_class = true
establish_connection(
# if you are using rails you can reference a custom environment via your database.yml
# otherwise you can manually specify the connection here like so:
:adapter => "",
:database => "",
:username => "",
:password => "",
:host => "",
:port => "")
end
# Located in models
# This model is from db2, set it to inherit db2_base, because that is what establishes the connection to the 2nd database
Class Db2Model < Db2Base
#if you are working with a legacy db and rails doesnt like the table names you can use self.table_name = 'name' to change it
# self.table_name = 'db2_model'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment