Connecting to multiple databases in rails in one application

I need to be able to have a single rails application that has models
from two different databases.Some models are in database 1 locally and
some are in database 2 in a remote server. I need to be able to have
models, migrations, fixtures and tests for all models and controllers
connected to the two different databases. I am using Rails 2 and I
really have a need for this to work.

Please point me in the direction of a plugin that will suit my needs.
This seems like something that should be allowed in rails by now with a
simple call:

def MyModel
:database_name => ‘second_database’
end

but nothing like this exists to my knowledge. Please help me. There has
got to be a solution.

The command you are looking for is establish_connection. You might
want to look at the rails rdoc. Search for “Connection to multiple
databases in different models” in
http://api.rubyonrails.com/classes/ActiveRecord/Base.html.

eg:

class SomeModelDB1 < ActiveRecord::Base
self.establish_connection(“db1”)
end

class SomeModelDB2 < ActiveRecord::Base
self.establish_connection(“db2”)
end

database.yml
db1:
adapter: mysql
database: somedb
username: user
password: pass
host: localhost
socket: /tmp/mysql.sock
db2:
adapter: mysql
database: somedb2
username: user
password: pass
host: remotehost

On Dec 10, 3:53 am, Nathan E. <rails-mailing-l…@andreas-

And this will allow me to have fixtures and tests for models that use a
different database?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs