<?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>Kickass Labs &#187; activerecord</title>
	<atom:link href="http://www.kickasslabs.com/tag/activerecord/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kickasslabs.com</link>
	<description>We &#9829; code.</description>
	<lastBuildDate>Wed, 28 Dec 2011 16:57:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Rails Gotcha: ActiveRecord Caches Associated Records by Default</title>
		<link>http://www.kickasslabs.com/2009/04/23/rails-gotcha-activerecord-caches-associated-records-by-default/</link>
		<comments>http://www.kickasslabs.com/2009/04/23/rails-gotcha-activerecord-caches-associated-records-by-default/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 03:00:00 +0000</pubDate>
		<dc:creator>gabe</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[active record]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=258</guid>
		<description><![CDATA[ActiveRecord will cache the results of association method calls by default, unless you tell it not to. (This applies to Rails 2.3.2 and perhaps earlier versions.) From the documentation: project.milestones # fetches milestones from the database project.milestones.size # uses the &#8230; <a href="http://www.kickasslabs.com/2009/04/23/rails-gotcha-activerecord-caches-associated-records-by-default/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>ActiveRecord will cache the results of association method calls by default, unless you tell it not to. </strong></p>
<p>(This applies to Rails 2.3.2 and perhaps earlier versions.)</p>
<p>From <a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html">the documentation:</a></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">project.<span style="color:#9900CC;">milestones</span>             <span style="color:#008000; font-style:italic;"># fetches milestones from the database</span>
project.<span style="color:#9900CC;">milestones</span>.<span style="color:#9900CC;">size</span>        <span style="color:#008000; font-style:italic;"># uses the milestone cache</span>
project.<span style="color:#9900CC;">milestones</span>.<span style="color:#9900CC;">empty</span>?      <span style="color:#008000; font-style:italic;"># uses the milestone cache</span>
project.<span style="color:#9900CC;">milestones</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">size</span>  <span style="color:#008000; font-style:italic;"># fetches milestones from the database</span></pre></div></div>

<p>Normally, this is great.  However, if you&#8217;re not aware of this default caching, you might see some strange behavior in your app or tests and have no idea what&#8217;s going on.  This default caching behavior had me and Abel stumped until we read about it in the docs.</p>
<p>Working with the same concept as the example in the documentation, where a Project has many Milestones, here&#8217;s a more explicit example of the caching behavior in action:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">project_1 = Project.<span style="color:#9900CC;">find_by_id</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>  
project_2 = Project.<span style="color:#9900CC;">find_by_id</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>  
<span style="color:#008000; font-style:italic;"># Load the same Project into two variables</span>
&nbsp;
project_1.<span style="color:#9900CC;">milestones</span>.<span style="color:#9900CC;">length</span>   
<span style="color:#008000; font-style:italic;"># - Hits the db's Milestones table.  </span>
<span style="color:#008000; font-style:italic;"># - Caches milestones object on project_1.</span>
<span style="color:#008000; font-style:italic;"># - Returns 0. </span>
&nbsp;
Milestone.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:project_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'New Milestone'</span><span style="color:#006600; font-weight:bold;">&#41;</span>  
<span style="color:#008000; font-style:italic;"># Adds a milestone to the project. </span>
<span style="color:#008000; font-style:italic;"># But we don't do it through the project_1.milestones association</span>
<span style="color:#008000; font-style:italic;"># because that _would_ update project_1.milestones's cached value</span>
&nbsp;
project_1.<span style="color:#9900CC;">milestones</span>.<span style="color:#9900CC;">length</span>   
<span style="color:#008000; font-style:italic;"># Returns 0 (not 1, like you'd expect) </span>
<span style="color:#008000; font-style:italic;"># because project_1.milestones was cached when </span>
<span style="color:#008000; font-style:italic;"># previously requested above.</span>
&nbsp;
project_2.<span style="color:#9900CC;">milestones</span>.<span style="color:#9900CC;">length</span>  
<span style="color:#008000; font-style:italic;"># Returns 1, because project_2.milestones </span>
<span style="color:#008000; font-style:italic;"># hasn't been requested/cached yet.</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Note: project_2.milestones is </span>
<span style="color:#008000; font-style:italic;"># a COMPLETELY DIFFERENT IN-MEMORY OBJECT </span>
<span style="color:#008000; font-style:italic;"># than project_1.milestones.</span>
<span style="color:#008000; font-style:italic;"># Taking this point further, here's an explicit example:</span>
&nbsp;
project_2.<span style="color:#9900CC;">milestones</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> Milestone.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Another Milestone'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># Put another milestone on the project through </span>
<span style="color:#008000; font-style:italic;"># project_2's milestones association.</span>
&nbsp;
project_1.<span style="color:#9900CC;">milestones</span>.<span style="color:#9900CC;">length</span>
<span style="color:#008000; font-style:italic;"># Still returns 0, because project_1 already cached it's copy of </span>
<span style="color:#008000; font-style:italic;"># the milestones association back when there were 0.</span>
&nbsp;
project_2.<span style="color:#9900CC;">milestones</span>.<span style="color:#9900CC;">length</span> 
<span style="color:#008000; font-style:italic;"># Now returns 2, because only project_2 knows about the new milestone.</span>
&nbsp;
project_1.<span style="color:#9900CC;">milestones</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">length</span>  
<span style="color:#008000; font-style:italic;"># Returns 2, because ActiveRecord updates the cache </span>
<span style="color:#008000; font-style:italic;"># when association(true) is present.</span></pre></div></div>

<p>Another funny something to note about the association caching behavior is that <strong>even when ActiveRecord uses a cached value, it still emits SQL to the log file</strong>.  So, don&#8217;t let that trip you up either.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F04%2F23%2Frails-gotcha-activerecord-caches-associated-records-by-default%2F&amp;title=Rails%20Gotcha%3A%20ActiveRecord%20Caches%20Associated%20Records%20by%20Default" id="wpa2a_2"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2009/04/23/rails-gotcha-activerecord-caches-associated-records-by-default/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t try to be smarter than ActiveRecord</title>
		<link>http://www.kickasslabs.com/2008/11/17/dont-try-to-be-smarter-than-activerecord/</link>
		<comments>http://www.kickasslabs.com/2008/11/17/dont-try-to-be-smarter-than-activerecord/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 15:52:20 +0000</pubDate>
		<dc:creator>gabe</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sanitization]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlite]]></category>

		<guid isPermaLink="false">http://www.kickasslabs.com/?p=81</guid>
		<description><![CDATA[When you&#8217;re writing conditions for a finder in ActiveRecord, and you want to use an array of values for a sql in() statement, you might think to help ActiveRecord out and comma-separate the array values like this: type_ids = [1, &#8230; <a href="http://www.kickasslabs.com/2008/11/17/dont-try-to-be-smarter-than-activerecord/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;re writing conditions for a finder in ActiveRecord, and you want to use an array of values for a sql in() statement, you might think to help ActiveRecord out and comma-separate the array values like this:</p>
<pre><code>type_ids = [1, 2, 3]
Something.find(
  :all,
  :conditions =&gt; ['type_id in (?)', type_ids.join(',') ] )
</code></pre>
<p>Which will generate a sql fragment that looks like this:</p>
<blockquote><p>[...] where events.status_id in (&#8217;1,2,3&#8242;) [...]</p></blockquote>
<p>Well, this will work just fine if you&#8217;re using MySQL, but not so fine (read &#8216;at all&#8217;) if you&#8217;re using SQLite.  So, if you&#8217;re using MySQL for your development environment and SQLite for your test environment, like me, it will work fine in dev and fail in your tests.</p>
<p>So, instead, just let ActiveRecord&#8217;s sanitizers do their job and write:</p>
<pre><code>Something.find(
  :all,
  :conditions =&gt; ['type_id in (?)', type_ids])</code></pre>
<p>Your sanity will thank you.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kickasslabs.com%2F2008%2F11%2F17%2Fdont-try-to-be-smarter-than-activerecord%2F&amp;title=Don%26%238217%3Bt%20try%20to%20be%20smarter%20than%20ActiveRecord" id="wpa2a_4"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kickasslabs.com/2008/11/17/dont-try-to-be-smarter-than-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

