You should simply specify your connection options either in the model or in database.yml. Let's go the 1st route initially:
# This is the new users table - connects to development/test/production
# DB from database.yml
class User < ActiveRecord::Base
end
class OldUser < ActiveRecord::Base
establish_connection :adapter => "postgresql",
:database => "legacy_users",
:username => "whatever",
:password => "something"
set_table_name "u_users" # Whatever you require
belongs_to :company, :class_name => "OldCompany", :foreign_key => "fk_company_id"
end
class OldCompany < ActiveRecord::Base
establish_connection :adapter => "postgresql",
:database => "legacy_users",
:username => "whatever",
:password => "something"
set_table_name "u_company" # Whatever you require
has_many :users, :class_name => "OldUser", :foreign_key => "fk_company_id"
end
From regular code, you use the models like you're used to:
OldUser.find_each do |ouser|
User.create!(:username => ouser.username, :company_name => ouser.company.name)
end
ActiveRecord will handle all the details for you.
Now, if you're like me, you don't like putting such level of details in your models - username, passwords, etc. Simple - move that config to database.yml and connect using the correct establish_connection syntax:
# database.yml
development:
adapter: postgresql
# go on as usual, for all 3 envs
legacy_users_development:
adapter: postgresql
database: legacy_users
username: whatever
password: something
Note the naming convention - legacy_users_#{Rails.env} is what I'm aiming for here, and here's how to do it:
class OldUser < ActiveRecord::Base
establish_connection "legacy_users_#{Rails.env}"
set_table_name "u_users" # Whatever you require
belongs_to :company, :class_name => "OldCompany", :foreign_key => "fk_company_id"
end
Bingo, everything else will work just fine.