<?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>Frenzic Mojo &#187; activerecord</title>
	<atom:link href="http://www.frenzicmojo.net/tag/activerecord/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.frenzicmojo.net</link>
	<description>A boy and his blog</description>
	<lastBuildDate>Tue, 10 Nov 2009 04:29:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Learn from My mistakes #1* (Model Validations)</title>
		<link>http://www.frenzicmojo.net/2009/01/learn-from-my-mistakes-1-model-validations/</link>
		<comments>http://www.frenzicmojo.net/2009/01/learn-from-my-mistakes-1-model-validations/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 23:16:32 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.frenzicmojo.net/?p=9</guid>
		<description><![CDATA[This one is pretty obvious, but I&#8217;m going to say it anyway: Do not, I repeat, do not, rely on model validations to protect your database integrity!
My example for this is a relatively minor one, but it could easily have been much worse:
During the early development of an app I worked on, we installed acts_as_taggable_on_steroids [...]]]></description>
			<content:encoded><![CDATA[<p>This one is pretty obvious, but I&#8217;m going to say it anyway: Do not, I repeat, do not, rely on model validations to protect your database integrity!</p>
<p>My example for this is a relatively minor one, but it could easily have been much worse:</p>
<p>During the early development of an app I worked on, we installed <a href="http://github.com/mattetti/acts_as_taggable_on_steroids/tree/master">acts_as_taggable_on_steroids</a> to handle tagging. The tag model, as defined in tag.rb has this line:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">validates_uniqueness_of <span style="color:#ff3333; font-weight:bold;">:name</span></pre></div></div>

<p>Makes sense, right? Because to have duplicates of any given tag would negate the effectiveness of tagging on a whole. Well, depending on how you had things set up anyway.</p>
<p>Enter problem:</p>
<p>One feature of the app was the ability to do a bulk import of a CSV file, including tags. Any of you who have tried to do bulk imports in Rails will probably know that the first thing you do after writing a working prototype, is to factor out any and all usage of ActiveRecord, because, as beautiful and powerful as ActiveRecord is, it&#8217;s dog slow when you&#8217;re trying to import and save a few thousand records.</p>
<p>For those of you who haven&#8217;t realized where this is going, circumventing ActiveRecord means that all of those neat little model validations never fire. Which means duplicate tag records just kept on piling up, until we finally noticed it and I had to waste time writing a migration to consolidate the tags and their associations.</p>
<p>What&#8217;s the solution?</p>
<p>Database constraints!</p>
<p>Are you using validates_uniqueness_of? Add a unique index to the table!**</p>
<p><span style="font-size:85%;">* No, it&#8217;s not my first mistake, but the first in what I&#8217;m sure will be many posts featuring stupid mistakes I&#8217;ve made that you should learn from.</span><br />
<span style="font-size:85%;">** This message brought to you by the DBA in me. Rails is fantastic, but I miss me some SQL.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.frenzicmojo.net/2009/01/learn-from-my-mistakes-1-model-validations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting Around AR</title>
		<link>http://www.frenzicmojo.net/2008/04/getting-around-ar/</link>
		<comments>http://www.frenzicmojo.net/2008/04/getting-around-ar/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 15:04:58 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.frenzicmojo.net/?p=3</guid>
		<description><![CDATA[So, this was a pain: I spent about 2 hours this weekend trying to find an easy way to execute a SQL select statement while circumventing the ActiveRecord model. After scouring the web and some old code, I found this:

Model.connection.select_rows&#40;sql&#41;

It returns an array of arrays, each inner array representing a record of data. Easy to [...]]]></description>
			<content:encoded><![CDATA[<p>So, this was a pain: I spent about 2 hours this weekend trying to find an easy way to execute a SQL select statement while circumventing the ActiveRecord model. After scouring the web and some old code, I found this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Model.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">select_rows</span><span style="color:#006600; font-weight:bold;">&#40;</span>sql<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>It returns an array of arrays, each inner array representing a record of data. Easy to do, and the results are easy to deal with.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.frenzicmojo.net/2008/04/getting-around-ar/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
