Multiple Database Connections in Ruby on Rails
18 comments Latest by Joe Thu, 26 Mar 2009 21:10:15 GMT
We have a client that already has some database replication going on in their deployment and needed to have most of their Ruby on Rails application pull from slave servers, but the few writes would go to the master, which would then end up in their slaves.
So, I was able to quickly extend ActiveRecord with just two methods to achieve this. Anyhow, earlier today, someone in #caboose asked if there was any solutions to this and it prompted me to finally package this up into a quick and dirty Rails plugin.
Introducing… Active Delegate!
To install, do the following:
cd vendor/plugins;
piston import http://svn.planetargon.org/rails/plugins/active_delegate
Next, you’ll need to create another database entry in your database.yml.
login: &login
adapter: postgresql
host: localhost
port: 5432
development:
database: rubyurl_development
<<: *login
test:
database: rubyurl_test
<<: *login
production:
database: rubyurl_servant
<<: *login
# NOTICE THE NEXT ENTRY/KEY
master_database:
database: rubyurl_master
<<: *login
At this point, your Rails application won’t talk to the master_database, because nothing is being told to connect to it. So, the current solution with Active Delegate is to create an ActiveRecord model that will act as a connection handler.
# app/models/master_database.rb
class MasterDatabase < ActiveRecord::Base
handles_connection_for :master_database # <-- this matches the key from our database.yml
end
Now, in the model(s) that we’ll want to have talk to this database, we’ll do add the following.
# app/models/animal.rb
class Animal < ActiveRecord::Base
delegates_connection_to :master_database, :on => [:create, :save, :destroy]
end
Now, when your application performs a create, save, or destroy, it’ll talk to the master database and your find calls will retrieve data from your servant database.
It’s late on a Friday afternoon and I felt compelled to toss this up for everyone. I think that this could be improved quite a bit, but it’s working great for the original problem that needed to be solved.
If you have feedback and/or bugs, please send us tickets.
Enjoying the content? Be sure to subscribe to my RSS feed.






Aah, pretty neat!
Awesome plugin, just what I was looking for ;)
A cursory glance at the code shows me that this will fail to handle collections properly. Setting a collection to an explicit list will cause a delete_all to be run. Deletes don’t have callbacks and therefor you will wind up hitting the slave. I have filed a bug.
Why wouldn’t you want this turned on for all your models? Is it possible to use delegates_connection_to for all of them?
Can you explain the “&” and ”<<” stuff? I think I’ve seen this before but have never been clear as to how it works.
Can you explain the “&” and ”<<” stuff? I think I’ve seen this before but have never been clear as to how it works.
I think you should use replication features provided by database rather than create one on your own. It will be safer and provide higher performance.
Not everything can be done by standard replication features. Thanks for the tip Robby!
Does this accomplish anything more than http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/ ?
@Frank
doing something like login: &login adaptor: mysql etc: etc
then development: database: hat_development <<: *login
So we use & to store a bunch of yaml, and <<: * to put it back in place. I’m not sure the actual code behind it - whether it’s an actual block or if it’s just yaml - but either way, that’s what it’s for.
Think it originally showed up on Redhanded?
Congratulations to writing your first blog post without “PLANET ARGON” in all caps in the body. That line always turns me off enough that I never read beyond it. This time I could read the whole post, and it was interesting. Thanks!
@Jeff: I don’t think it does any more than Dr. Nic’s magic multi connections, but sounds like it has more issues. However, if you are really trying to handle replicated databases seriously I quite like the approach taken in MySQL Replication Adapter project (http://rubyforge.org/projects/mysql-replicate/). It feels much more complete than doing noob AR method extensions to attempt something much larger.
Superb plugin! I had to change mysqldump to mysqldump5 and added the support to only dump the structure of the database “mysql-style” not schema.rb:
Is this sufficient to connect two databases in one rails app? Could you please explain ? I am experiencing the same issue so need your help?
Thanks Saket
Is this sufficient to connect two databases in one rails app? Could you please explain ? I am experiencing the same issue so need your help?
Thanks Saket
nice share
Robby thanks for a great plugin!
I’ve only changed toCool stuff. How does this affect testing with multiple databases?