<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Workin' on it. &#187; association</title>
	<atom:link href="http://jameshalberg.wordpress.com/tag/association/feed/" rel="self" type="application/rss+xml" />
	<link>http://jameshalberg.wordpress.com</link>
	<description></description>
	<lastBuildDate>Sat, 19 Nov 2011 14:15:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jameshalberg.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Workin' on it. &#187; association</title>
		<link>http://jameshalberg.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jameshalberg.wordpress.com/osd.xml" title="Workin&#039; on it." />
	<atom:link rel='hub' href='http://jameshalberg.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Is an association already loaded?</title>
		<link>http://jameshalberg.wordpress.com/2008/01/30/is-an-association-already-loaded/</link>
		<comments>http://jameshalberg.wordpress.com/2008/01/30/is-an-association-already-loaded/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 03:25:59 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[association]]></category>

		<guid isPermaLink="false">http://jameshalberg.wordpress.com/?p=107</guid>
		<description><![CDATA[If you can&#8217;t tell&#8230; I like posting little &#8220;cheatsheet&#8221; items here. So, here&#8217;s another one that I&#8217;ve bumped into again recently after not needing it for a while. You want to know how many Items are currently in the Store &#8230; <a href="http://jameshalberg.wordpress.com/2008/01/30/is-an-association-already-loaded/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.wordpress.com&amp;blog=160606&amp;post=107&amp;subd=jameshalberg&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you can&#8217;t tell&#8230; I like posting little &#8220;cheatsheet&#8221; items here.  So, here&#8217;s another one that I&#8217;ve bumped into again recently after not needing it for a while.</p>
<p>You want to know how many Items are currently in the Store and you&#8217;ve got this in your Store definition:<br />
<code>has_many :items</code></p>
<p>If the Items for this Store have already been read in and objects have been mapped, you simply want to ask for the count:<br />
<code>flagship_store.items.size</code></p>
<p>but if they haven&#8217;t already been mapped, you want to just do a quick select count(*):<br />
<code>Item.count(:conditions =&gt; ["store_id = ?", flagship_store.id])</code></p>
<p>The answer?</p>
<p>For many of us it&#8217;s:<br />
<code><br />
item_count = flagship_store.instance_variable_get(:@items) ? @items.size : Item.count(:conditions =&gt; ["store_id = ?", flagship_store.id])<br />
</code></p>
<p>And for those of you living in the future it&#8217;s even more straightforward:<br />
<code><br />
item_count = flagship_store.instance_variable_defined?(:@items) ? @items.size : Item.count(:conditions =&gt; ["store_id = ?", flagship_store.id])<br />
</code></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jameshalberg.wordpress.com/107/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jameshalberg.wordpress.com/107/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshalberg.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshalberg.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshalberg.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshalberg.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jameshalberg.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jameshalberg.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jameshalberg.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jameshalberg.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshalberg.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshalberg.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshalberg.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshalberg.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshalberg.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshalberg.wordpress.com/107/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.wordpress.com&amp;blog=160606&amp;post=107&amp;subd=jameshalberg&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jameshalberg.wordpress.com/2008/01/30/is-an-association-already-loaded/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jameshalberg</media:title>
		</media:content>
	</item>
	</channel>
</rss>
