<?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>this oughta be interesting... &#187; active record</title>
	<atom:link href="http://joshsharpe.com/archives/category/active-record/feed" rel="self" type="application/rss+xml" />
	<link>http://joshsharpe.com</link>
	<description></description>
	<lastBuildDate>Tue, 06 Dec 2011 17:52:11 +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>nested relations in ActiveRecord</title>
		<link>http://joshsharpe.com/archives/nested-relations-in-activerecord</link>
		<comments>http://joshsharpe.com/archives/nested-relations-in-activerecord#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:09:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[active record]]></category>

		<guid isPermaLink="false">http://joshsharpe.com/?p=56</guid>
		<description><![CDATA[I personally don&#8217;t think this is all that exciting &#8212; but I see this question asked a lot and just want something to point at from now on. Say you have several &#8216;nested&#8217; has_many relations: class State < ActiveRecord::Base has_many :cities end class City < ActiveRecord::Base has_many :streets belongs_to :state end class Street < ActiveRecord::Base [...]]]></description>
			<content:encoded><![CDATA[<p>I personally don&#8217;t think this is all that exciting &#8212; but I see this question asked a lot and just want something to point at from now on.</p>
<p>Say you have several &#8216;nested&#8217; has_many relations:</p>
<pre>
class State < ActiveRecord::Base
  has_many :cities
end

class City < ActiveRecord::Base
  has_many :streets
  belongs_to :state
end

class Street < ActiveRecord::Base
  has_many :houses
  belongs_to :city
end

class House < ActiveRecord::Base
  belongs_to :street
end
</pre>
<p>How do you get all of the Houses in virginia?</p>
<p>Well, you could do this:</p>
<pre>
virginia.cities.collect{|c| c.streets}.flatten.uniq.collect{|s| s.houses}.flatten.uniq
</pre>
<p>... but that's epically lame.  It looks like shit and produces an metric ton of queries and as a result is highly inefficient.</p>
<p>How about this:</p>
<pre>
House.find(
  :all,
  :include => {:street => {:city => :state}},
  :conditions => {'states.id' => virginia.id}
)
</pre>
<p>This is a single query with joins where appropriate, and it's a finder on House which is what you're getting anyways.  Makes sense, no?</p>
]]></content:encoded>
			<wfw:commentRss>http://joshsharpe.com/archives/nested-relations-in-activerecord/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>the single best active record learning tool that exists. period. the end.</title>
		<link>http://joshsharpe.com/archives/the-single-best-active-record-learning-tool-that-exists-period-the-end</link>
		<comments>http://joshsharpe.com/archives/the-single-best-active-record-learning-tool-that-exists-period-the-end#comments</comments>
		<pubDate>Tue, 27 Oct 2009 22:17:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[active record]]></category>

		<guid isPermaLink="false">http://joshsharpe.com/?p=3</guid>
		<description><![CDATA[Every once in a while I see this little code snippet show up somewhere: script_console_running = ENV.include?('RAILS_ENV') &#38;&#38; IRB.conf[:LOAD_MODULES] &#38;&#38; IRB.conf[:LOAD_MODULES].include?('console_with_helpers') rails_running = ENV.include?('RAILS_ENV') &#38;&#38; !(IRB.conf[:LOAD_MODULES] &#38;&#38; IRB.conf[:LOAD_MODULES].include?('console_with_helpers')) irb_standalone_running = !script_console_running &#38;&#38; !rails_running if script_console_running require 'logger' Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT)) end You see, if you drop this little gem in ~/.irbrc you&#8217;ll start getting the SQL [...]]]></description>
			<content:encoded><![CDATA[<p>Every once in a while I see this little code snippet show up somewhere:</p>
<pre>
script_console_running = ENV.include?('RAILS_ENV') &amp;&amp;
                         IRB.conf[:LOAD_MODULES] &amp;&amp;
                         IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
rails_running = ENV.include?('RAILS_ENV') &amp;&amp;
                !(IRB.conf[:LOAD_MODULES] &amp;&amp;
                IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
irb_standalone_running = !script_console_running &amp;&amp; !rails_running
if script_console_running
  require 'logger'
  Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end
</pre>
<p>You see, if you drop this little gem in ~/.irbrc you&#8217;ll start getting the SQL output from your various ruby commands.  This is an excellent <em>debugging</em> tool, but it&#8217;s an even better <em>learning</em> tool.</p>
<p>To the novice the difference between the following two statements may not be readily apparent&#8230;</p>
<pre>
User.find(:first).posts.each{|p| p.comments.do_something}
User.find(:first, :include => {:posts => :comments}).posts.each{|p| p.comments.do_something}
</pre>
<p>&#8230;but as soon as they sit and watch what scrolls by in the terminal they will *very* quickly realize that one of them is *very* wrong.</p>
<p>I submit that the next time you are teaching anyone the basics of AR that this be the very first thing that you introduce them to.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshsharpe.com/archives/the-single-best-active-record-learning-tool-that-exists-period-the-end/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

