Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting Rails 3.1 with Multiple Databases

At ShowNearby we have been doing a very big migration to RoR 3.1 from PHP and we are facing several problems that may be some of you have solved before.

We have big amounts of data and we decided to segregate our DB into several DBs that we can handle separately. For example, our accounts, places, logs and others are split into several databases

We need to get migrations, fixtures, models, to play nicely, and so far it has been quite messy. Some of our requirements for a solution to be acceptable:

  • one model should relate to one tables in one of the databases.
  • rake db:drop - should drop all the database env we specify in database.yml
  • rake db:create - should create all the database env we specify in database.yml
  • rake db:migrate - should run migrations to the various databases
  • rake db:test - should grab fixtures and drop them into the various databases and test unit/function/etc

We are considering setting separate rails projects per each database and connecting them with ActiveResource, but we feel this is not very efficient. Have any of you deal with a similar problem before?

like image 623
Fer Martin Avatar asked May 25 '11 09:05

Fer Martin


People also ask

Is it possible to connect to multiple databases simultaneously?

This can be done several times to connect to different databases, with the restriction that it will only allow one connection to the same database. If you try to use a database from multiple instances of the same application either on the same computer or on different computers you will receive an error message.

Is it possible to use two databases in a single application in Ruby on Rails?

Rails now has support for multiple databases so you don't have to store your data all in one place. At this time the following features are supported: Multiple writer databases and a replica for each. Automatic connection switching for the model you're working with.

Can we connect to two database in a web application?

Yes. Give it a go. "Is it possible to connect multiple database with one application." Yes.


1 Answers

To Wukerplank's answer, you can also put the connection details in database.yml like usual with a name like so:

log_database_production:   adapter: mysql   host: other_host   username: logmein   password: supersecret   database: logs 

Then in your special model:

class AccessLog < ActiveRecord::Base   establish_connection "log_database_#{Rails.env}".to_sym end 

To keep those pesky credentials from being in your application code.

Edit: If you want to reuse this connection in multiple models, you should create a new abstract class and inherit from it, because connections are tightly coupled to classes (as explained here, here, and here), and new connections will be created for each class.

If that is the case, set things up like so:

class LogDatabase < ActiveRecord::Base   self.abstract_class = true   establish_connection "log_database_#{Rails.env}".to_sym end  class AccessLog < LogDatabase end  class CheckoutLog < LogDatabase end 
like image 143
Unixmonkey Avatar answered Nov 15 '22 23:11

Unixmonkey