CONNECT 2 DATABASES IN A SINGLE RAILS APPLICATION

Posted on June 22, 2011

0


Many a times , the need of the situation is to connect ,multiple databases to a single rails app.

This seems to be a bit complex issue since we are all used to connect a single database to a rails app.

But after some googling, I found out that in rails, it’s a damn simple thing.

I will show you how I did it.

I have a rails app called “multidbtest1”.

It has a table called as User.

I wanted to connect it to another database.So I created another app called “multidbtest3

It had a table called Customer.

My requirement is to use Customer table in multibdtest1.

We know that database connections are handled in database.yml file.

So we copy the contents of database.yml of multidbtest3 in database.yml of multidbtest1.

Thus, the database.yml file of multidbtest1 looks like:

# SQLite version 3.x

#   gem install sqlite3

development:

  adapter: sqlite3

  database: db/development.sqlite3

  pool: 5

  timeout: 5000

# Warning: The database defined as “test” will be erased and

# re-generated from your development database when you run “rake”.

# Do not set this db to the same as development or production.

test:

  adapter: sqlite3

  database: db/test.sqlite3

  pool: 5

  timeout: 5000

production:

  adapter: sqlite3

  database: db/production.sqlite3

  pool: 5

  timeout: 5000

—————————-copied content of multidbtest3—————————-

 #SQLite version 3.x

#   gem install sqlite3

development:

  adapter: sqlite3

  database: db/development.sqlite3

  pool: 5

  timeout: 5000

# Warning: The database defined as “test” will be erased and

# re-generated from your development database when you run “rake”.

# Do not set this db to the same as development or production.

test:

  adapter: sqlite3

  database: db/test.sqlite3

  pool: 5

  timeout: 5000

production:

  adapter: sqlite3

  database: db/production.sqlite3

  pool: 5

  timeout: 5000

But this seems to be obviously wrong, so we make some changes.In the database field , we must specify the name and path of the database.The modifies file looks like:

# SQLite version 3.x

#   gem install sqlite3

development:

  adapter: sqlite3

  database: db/development.sqlite3

  pool: 5

  timeout: 5000

# Warning: The database defined as “test” will be erased and

# re-generated from your development database when you run “rake”.

# Do not set this db to the same as development or production.

test:

  adapter: sqlite3

  database: db/test.sqlite3

  pool: 5

  timeout: 5000

production:

  adapter: sqlite3

  database: db/production.sqlite3

  pool: 5

  timeout: 5000

 

—————————copied content of multidbtest3—————————-

 #SQLite version 3.x

#   gem install sqlite3

multidbtest3_development:

  adapter: sqlite3

  database: C:/Users/Rushabh/RubymineProjects/multidbtest3/db/development.sqlite3

  pool: 5

  timeout: 5000

# Warning: The database defined as “test” will be erased and

# re-generated from your development database when you run “rake”.

# Do not set this db to the same as development or production.

multidbtest3_test:

  adapter: sqlite3

  database: :/Users/Rushabh/RubymineProjects/multidbtest3/db/test.sqlite3

  pool: 5

  timeout: 5000

multidbtest3_production:

  adapter: sqlite3

  database: :/Users/Rushabh/RubymineProjects/multidbtest3/db/production.sqlite3

  pool: 5

  timeout: 5000

That’s all!!  The database part is done.

Now how shall we use the database ?

Its even simpler.

Create a model class called “Customer”

The model file should look like :

class Customer < ActiveRecord::Base 

  establish_connection “multidbtest3_#{Rails.env}” 

end

That’s it.

Now in any controller we can access the contents of Customer table.

For eg :

@customers = Customer.find(:all)

Enjoy

Advertisements
Posted in: Database, Rails app