<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: Multiple Database Connections in Ruby on Rails</title>
    <link>http://www.robbyonrails.com/articles/2007/10/05/multiple-database-connections-in-ruby-on-rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Multiple Database Connections in Ruby on Rails</title>
      <description>&lt;p&gt;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.&lt;/p&gt;


	&lt;p&gt;So, I was able to quickly extend ActiveRecord with just &lt;em&gt;two&lt;/em&gt; 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.&lt;/p&gt;


	&lt;p&gt;Introducing&amp;#8230; &lt;strong&gt;Active Delegate&lt;/strong&gt;!&lt;/p&gt;


	&lt;p&gt;To install, do the following:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
cd vendor/plugins;
piston import http://svn.planetargon.org/rails/plugins/active_delegate
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Next, you&amp;#8217;ll need to create another database entry in your &lt;code&gt;database.yml&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
login: &amp;#38;login
  adapter: postgresql
  host: localhost
  port: 5432

development:
  database: rubyurl_development
  &amp;lt;&amp;lt;: *login

test:
  database: rubyurl_test
  &amp;lt;&amp;lt;: *login

production:
  database: rubyurl_servant
  &amp;lt;&amp;lt;: *login

# NOTICE THE NEXT ENTRY/KEY
master_database:
  database: rubyurl_master
  &amp;lt;&amp;lt;: *login
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;At this point, your Rails application won&amp;#8217;t talk to the &lt;code&gt;master_database&lt;/code&gt;, 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.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # app/models/master_database.rb
  class MasterDatabase &amp;lt; ActiveRecord::Base
    handles_connection_for :master_database # &amp;lt;-- this matches the key from our database.yml
  end  
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, in the model(s) that we&amp;#8217;ll want to have talk to this database, we&amp;#8217;ll do add the following.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # app/models/animal.rb
  class Animal &amp;lt; ActiveRecord::Base
     delegates_connection_to :master_database, :on =&amp;gt; [:create, :save, :destroy]
  end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, when your application performs a &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;save&lt;/code&gt;, or &lt;code&gt;destroy&lt;/code&gt;, it&amp;#8217;ll talk to the master database and your &lt;code&gt;find&lt;/code&gt; calls will retrieve data from your servant database.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;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&amp;#8217;s working great for the original problem that needed to be solved.&lt;/p&gt;


	&lt;p&gt;If you have feedback and/or bugs, please &lt;a href="http://planetargon.lighthouseapp.com/projects/5187-open-source-projects/"&gt;send us tickets&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Fri, 05 Oct 2007 17:54:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:69a8625d-e24b-4f4e-aa58-d69a67784698</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/10/05/multiple-database-connections-in-ruby-on-rails</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>PostgreSQL</category>
      <category>PLANET ARGON</category>
      <category>databases</category>
      <category>replication</category>
      <category>activerecord</category>
      <category>plugins</category>
      <category>planetargon</category>
      <category>code</category>
    </item>
  </channel>
</rss>
