Ruby on Rails connect to Multiple Databases and using ActiveRecord with multiple databases, it’s really simple take it easy. Let’s run through this.
Rake Tasks
Well, I want to handle migrations for two databases, so I need two separate Rake tasks to handle that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
My first task is db:migrate
that delegates out to db:migrate_db1
& db:migrate_db2
.
Each of those establish a connection to the database and then runs the migrations from their own separate folders. This allows you to store migrations in separate folders so you can easily manage them.
The migrations are exactly the same as normal.
Database Connections
In order to get those migrations to work, I need to configure the database connections. I’m going to define everything in the database.yml
just like normal, but with a different naming convention:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
I configure two separate databases db1 & db2.
Then I need to configure the app to load these now. I open application.rb
or environment file(s):
1 2 3 4 5 6 |
|
Take a look at what’s going on:
- I set the database configuration to use. You can just use Rails.env here instead of ENV[‘ENV’].
- I load up the database.yml config and parse it with YAML.
- I grab the configuration from the file for each db and the correct environment that I’m running in.
Connecting Models
When I’m working with multiple databases, I like to explicitly setup the connections inside the models themselves instead of inheriting from ActiveRecord::Base and using subclasses.
1 2 3 |
|
1 2 3 |
|
Well, All you really need to do is load the configurations, establish the database connections, and setup the migrations to load from a specific directory for each database.
So far so good, See ya! :)