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?
- 
                    Possible duplicate of : http://stackoverflow.com/questions/6122508/connecting-rails-3-1-with-multiple-databases – voondo Sep 19 '14 at 08:20
- 
                    http://imnithin.github.io/multiple-database.html – Nithin Feb 28 '15 at 07:36
3 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 :) .
 
    
    - 4,480
- 3
- 27
- 27
 
    
    - 5,686
- 1
- 18
- 23
- 
                    6On Rails 5.1 I had to symbolize the connection name. `establish_connection "#{Rails.env}_sec".to_sym` – delphaber Jun 01 '17 at 09:17
- 
                    1Without symbolization, I got error `ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter1.` – Veck Hsiao Sep 30 '18 at 19:07
- 
                    
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(...)
 
    
    - 35,165
- 3
- 73
- 81
- 
                    1Thank you! Connecting to a separate database outside of an active record class requires the `ActiveRecord::Base.configurations['other_env']`, otherwise it doesn't actually load the configuration. – KPheasey Jun 10 '17 at 00:00
You might like to note that as of Rails 6 (2019), Rails has support for multiple primary databases!
https://guides.rubyonrails.org/active_record_multiple_databases.html
The database.yml file will now look something like this:
development:
  primary:
    database: primary_db
    user: root
  primary_replica:
    database: primary_db
    user: ro_user
    replica: true
  animals:
    database: my_animals_db
    user: root
    migrations_path: db/animals_migrate
  animals_replica:
    database: my_animals_db
    user: ro_user
    replica: true
And then it's as simple as specifying in your model files:
class AnimalsModel < ApplicationRecord
  self.abstract_class = true
  connects_to database: { writing: :animals_primary, reading: :animals_replica }
end
class Dog < AnimalsModel
  # connected to both the animals_primary db for writing and the animals_replica for reading
end
(These examples were taken from this helpful tutorial.)
 
    
    - 3,610
- 3
- 26
- 26
