<?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>worldgonemad.com &#187; Ruby</title>
	<atom:link href="http://worldgonemad.com/blogs/category/open-source/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://worldgonemad.com/blogs</link>
	<description>If the world was perfect, it wouldn&#039;t be.      - Yogi Berra</description>
	<lastBuildDate>Wed, 05 Jan 2011 20:42:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Reusing models outside of Rails</title>
		<link>http://worldgonemad.com/blogs/pantoniades/2010/01/reusing-models-outside-of-rails/</link>
		<comments>http://worldgonemad.com/blogs/pantoniades/2010/01/reusing-models-outside-of-rails/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 12:34:17 +0000</pubDate>
		<dc:creator>pantoniades</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://worldgonemad.com/blogs/pantoniades/2010/01/reusing-models-outside-of-rails/</guid>
		<description><![CDATA[Using rails models and activerecord in standalone ruby scripts
]]></description>
			<content:encoded><![CDATA[<p>If you have done a good job of building your rails models, you may find that they are helpful for your non-rails system maintenance and such. They may even be <i>necessary</i> to reuse if you follow the rails model of using activerecord validations (rather that database RI) to preserve the integrity of your data. </p>
<p>Or you may just find yourself rewriting the same code again and again, and want all that good railsiness to make it easier to write and maintain. Personally I find myself in some instance of ./script/console as often as irb just so I can get the activesupport helper methods ( <font face="courier new,courier,monospace">4.days.from_now</font> and such) that many rails developers are surprised to find are not actually a standard part of ruby. </p>
<p>So, the good news is it is easy to reuse rails code outside of rails. </p>
<p>Let&#8217;s say you want to do some data manipulation (reporting, loading, scrubbing, etc) in your rails db, and want to use your models to do it. A few imports in your ruby script gets the necessary environment in place:</p>
<p><font face="courier new,courier,monospace">require &#8216;rubygems&#8217;<br />require &#8216;yaml&#8217;<br />require &#8216;active_record&#8217;<br />require &#8216;logger&#8217;</font></p>
<p>and a few more will load up your models (note: they&#8217;re probably not in the same location as mine, unless you are also working on an app called &#8216;seweb&#8217; in your home dir):</p>
<p><font face="courier new,courier,monospace">PROJECT_HOME = &quot;#{ENV['HOME']}/seweb/&quot;<br />require &quot;#{PROJECT_HOME}/app/models/sales_rep.rb&quot;<br />require &quot;#{PROJECT_HOME}/app/models/organization.rb&quot;<br />require &quot;#{PROJECT_HOME}/app/models/team.rb&quot;</font></p>
<p>Then connect to the appropriate database (note I&#8217;m connecting to the development environment &#8211; can you guess how I&#8217;d connect to &#8216;test&#8217; or &#8216;production&#8217;?), with rails logging enabled:</p>
<p><font face="courier new,courier,monospace">ActiveRecord::Base.logger = Logger.new( STDERR )<br />db_config = YAML::load( File.open(&quot;#{seweb_home}/config/database.yml&quot;))<br />ActiveRecord::Base.establish_connection( db_config[&quot;development&quot;])</font></p>
<p>And you are good! If you are using a transactional database (such as my personal favorite, MySQL with InnoDB), you can make nice transaction wrappers for your work thusly:</p>
<p><font face="courier new,courier,monospace">ActiveRecord::Base.transaction do</font></p>
<p><font face="courier new,courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rep = SalesRep.find_or_initialize_by_name( &#8216;Kyllin D. Quota&#8217; )<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # create the component parts<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if( rep.changed? )<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; rep.organization = Organization.find_or_create_by_name &#8216;APAC&#8217;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; rep.team = Team.find_or_create_by_name &#8216;Enterprise&#8217;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; rep.save!<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; end</font></p>
<p><font face="courier new,courier,monospace">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; rescue Exception<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; raise ActiveRecord::Rollback, &quot;Invalid record for #{rep.name}&quot;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; end</font></p>
<p><font face="courier new,courier,monospace">end</font></p>
<p>Pow. You get your rails sugar, rails validations, rails logging. Are you happy? Why yes, yes you are.</p>
]]></content:encoded>
			<wfw:commentRss>http://worldgonemad.com/blogs/pantoniades/2010/01/reusing-models-outside-of-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

