One of my colleague has done multiple db connection in rails
Here is his work,
From, http://blog.brijeshshah.com/multiple-database-connections-in-rails/
it's really helpful.
Here is his work,
First set connection parameters for both database with different constant variables and define in environment file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | DB1 = { :adapter => 'mysql' , :database => DATABASE1 , :username => USERNAME , :password => PASSWORD , :host => HOST } DB2 = { :adapter => 'mysql' , :database => DATABASE2 , :username => USERNAME , :password => PASSWORD , :host => HOST } |
Add connection.rb file in lib folder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | module Connection def self .included(base) base.class_eval do parameters = self :: DB ActiveRecord::Base.establish_connection( :adapter => parameters[ :adapter ], :host => parameters[ :host ], :username => parameters[ :username ], :password => parameters[ :password ], :database => parameters[ :database ] ) end end end |
include connection file and set DATABASE in models
in Company model
1 2 | DB = DB1 include Connection |
in Website model
1 2 3 | DB = DB2 include Connection |
From, http://blog.brijeshshah.com/multiple-database-connections-in-rails/
it's really helpful.