Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to multiple databases in ruby on rails

I have a ruby on rails application working fine and connected to a database. Now i want to connect to a different database from the same application. The data model can be exactly the same. In fact if i connect to the different database the application works fine. However I want to connect to two different databases. Is it possible in ruby on rails?

like image 406
user1428970 Avatar asked Jun 26 '13 03:06

user1428970


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.

Can we connect two database in single application?

If you can work with single database, working with multiple is no different. You will need a connection string for each database. There rest is, as they say it, history.


2 Answers

For multiple database connection, you need to add the following codes to the database.yml file. Here, I am giving the example of connecting two databases from a rails application

config/database.yml

development:   adapter: mysql2   database: db1_dev   username: root   password: xyz   host: localhost  development_sec:   adapter: mysql2   database: db2_dev   username: root   password: xyz   host: localhost  production:   adapter: mysql2   database: db1_prod   username: root   password: xyz   host: your-production-ip  production_sec:   adapter: mysql2   database: db2_prod   username: root   password: xyz   host: your-production-ip 

Here I have used two databases for the development and production environment.

Now we need to connect the model to databases. When you are running your application in development and production mode, all the models will be mapped through the development and production db parameters those been mentioned in your database.yml. So for some model we need to connect to other database.

Lets assume that, we have two models User and Category. The users table is in db1_dev and db1_prod, the categories table in db2_dev and db2_prod.

Category model

class Category < ActiveRecord::Base   establish_connection "#{Rails.env}_sec".to_sym end 

Similarly, when you adding the new migration for the second database, need to add following code to it.

class CreateRewards < ActiveRecord::Migration   def connection     ActiveRecord::Base.establish_connection("#{Rails.env}_sec".to_sym).connection   end    def change     # your code goes here.   end end 

Hope it will work for you :) .

like image 86
Bachan Smruty Avatar answered Sep 20 '22 15:09

Bachan Smruty


Use establish_connection to switch to a different database:

ActiveRecord::Base.establish_connection(   :adapter  => "mysql",   :host     => "localhost",   :username => "myuser",   :password => "mypass",   :database => "somedatabase" ) 

You can also pass a preconfigured environment from database.yml like so:

ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['other_env']) 

You can also set it for a specific model:

MyClass.establish_connection(...) 
like image 27
PinnyM Avatar answered Sep 21 '22 15:09

PinnyM