<?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; active record</title>
	<atom:link href="http://www.kickasslabs.com/tag/active-record/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kickasslabs.com</link>
	<description>We &#9829; code.</description>
	<lastBuildDate>Sun, 04 Jul 2010 03:00:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 [...]]]></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 addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.kickasslabs.com%2F2009%2F04%2F23%2Frails-gotcha-activerecord-caches-associated-records-by-default%2F&amp;linkname=Rails%20Gotcha%3A%20ActiveRecord%20Caches%20Associated%20Records%20by%20Default"><img src="http://www.kickasslabs.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></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>
	</channel>
</rss>
