<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.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>Rubynaut: How to access multiple databases with rails</title>
    <link>http://www.rubynaut.net/articles/2008/05/31/how-to-access-multiple-database-in-rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>How to access multiple databases with rails</title>
      <description>&lt;p&gt;I know it exists other information about this, but I want to summarize here the practise I use in several project now. It takes 4 steps:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;configure your database.yml&lt;/li&gt;
		&lt;li&gt;create an abstract class for connection&lt;/li&gt;
		&lt;li&gt;create new models within a module&lt;/li&gt;
		&lt;li&gt;interact the 2 databases&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h3&gt;1. Configure your database.yml&lt;/h3&gt;


	&lt;p&gt;This is the same as you create production or development connection, just give it a specific name:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;other_database_connection:
    adapter: postgresql
    encoding: utf8
    database: database_name
    username: user
    password: password
    host: localhost&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h3&gt;2. Create an abstract class for connection&lt;/h3&gt;


	&lt;p&gt;Then create an abstract class you will inheritate by your new classes:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class External &amp;lt; ActiveRecord::Base
  self.abstract_class = true
  establish_connection :other_database_connection
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h3&gt;3. Create new models within a module&lt;/h3&gt;


	&lt;p&gt;To make it easier to use and cleaner, create a subfolder &lt;i&gt;OtherDatabase&lt;/i&gt; and then create in this folder your new classes (for each table you need) as following:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class OtherDatabase::NewClass1 &amp;lt; External
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h3&gt;4. How to interact the 2 databases&lt;/h3&gt;


	&lt;p&gt;Once you have created your new classes, you can simply access it using the module prefix:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;new_object = OtherDatabase::NewClass1.new
.
result = OtherDatabase::NewClass1.find(:all)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h3&gt;Conclusion&lt;/h3&gt;


	&lt;p&gt;That&amp;#8217;s all, submodule is not really necessary but keeps your model folder cleaner. Any suggestion to improve this practise ?&lt;/p&gt;</description>
      <pubDate>Sat, 31 May 2008 19:08:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:cdab0bde-77f9-4bc3-a266-f307e51dff36</guid>
      <author>stephane</author>
      <link>http://www.rubynaut.net/articles/2008/05/31/how-to-access-multiple-database-in-rails</link>
    </item>
    <item>
      <title>"How to access multiple databases with rails" by Tom</title>
      <description>&lt;p&gt;Do you have some suggestions for doing unit testing?  Fixtures?  I&amp;#8217;m running into issues with my models that exist in another database.&lt;/p&gt;</description>
      <pubDate>Fri, 15 Aug 2008 02:06:04 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:6d543e33-ec56-4666-8fa4-d4ee93a81cee</guid>
      <link>http://www.rubynaut.net/articles/2008/05/31/how-to-access-multiple-database-in-rails#comment-2044</link>
    </item>
  </channel>
</rss>
