<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Infernus &#187; Ruby</title>
	<atom:link href="http://infernus.org/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://infernus.org</link>
	<description>Don&#039;t feel you have to take any notice of me, please.</description>
	<lastBuildDate>Sat, 21 Apr 2012 13:55:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Two Datasources, One Rails Application</title>
		<link>http://infernus.org/2010/03/two-datasources-one-rails-application/</link>
		<comments>http://infernus.org/2010/03/two-datasources-one-rails-application/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 19:27:51 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://infernus.org/?p=380</guid>
		<description><![CDATA[Rails has a habit of lulling you into a false sense of security. The ease of use for common tasks makes you wonder why you&#8217;re wasting your time with Spring or JEE. Then you try to do something off the common path and watch Rails just shrug its shoulders and grin. I was working on [...]]]></description>
			<content:encoded><![CDATA[<p>
   Rails has a habit of lulling you into a false sense of security. The ease of use for common tasks makes you wonder why you&#8217;re wasting your time with Spring or JEE. Then you try to do something off the common path and watch Rails just shrug its shoulders and grin.
</p>
<p>
	I was working on a community project recently and ran into the requirement to access some static data in another database. Multiple datasources, easy enough. Unfortunately, Rails expects you&#8217;ll be sticking with just one, and so I had to go hunting for a solution. Thankfully, I wasn&#8217;t the first to hit this and so many people had already done the hard work, leaving me with my piecing together the most elegant solution.
</p>
<p>
	The first thing to do is define your datasource, presumably in <i>database.yml</i>.
</p>
<pre>
legacy_datasource:
  adapter: mysql
  database: legacy_database
  timeout: 5000
  encoding: utf8
  host: localhost
  username: readonly
  enable_call: true
  password: readonly
</pre>
<p>
	You&#8217;ll then need to introduce a base class for the models which will use this datasource. It&#8217;ll need to be abstract, to avoid declaring any columns and establish a connection to the secondary source.
</p>
<pre>
class LegacyObject < ActiveRecord::Base
  establish_connection :legacy_datasource

  self.abstract_class = true

  def self.columns() @columns ||= []; end

  def self.column(name, sql_type = nil, default = nil, null = true)
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
  end

end
</pre>
<p>
	Now build the model classes you require, making sure to extend from your new base class.
</p>
<pre>
class AnObject < LegacyObject

  column :object_id, :integer

  # and so on...

end
</pre>
<p>
	If you just require read-only access then you're now done. If you require migration support then a bit more work is needed.
</p>
<p>
	Create a new base class for the migrations that redefines the connection.
</p>
<pre>
class LegacyMigration < ActiveRecord::Migration
  def self.connection
    LegacyDatasource.connection
  end
end
</pre>
<p>
	Now, extend from the new base class and write your migration as normal.
</p>
<pre>
class AMigration < LegacyMigration
  def self.up
    # ...
  end

  def self.down
    # ...
  end
end
</pre>
<p>
	And you now have a complete solution. Despite the lack of official documentation, it's surprisingly easy to do.</p>
]]></content:encoded>
			<wfw:commentRss>http://infernus.org/2010/03/two-datasources-one-rails-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: infernus.org @ 2012-05-22 23:47:35 -->
