<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.1" -->
<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/"
	>

<channel>
	<title>Usergenic</title>
	<link>http://www.usergenic.com</link>
	<description>Humane Software Development</description>
	<pubDate>Wed, 29 Aug 2007 16:34:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>
	<language>en</language>
			<item>
		<title>Preserving with_scope on eager-loaded Associations</title>
		<link>http://www.usergenic.com/2007/08/27/preserving-with_scope-on-eager-loaded-associations/</link>
		<comments>http://www.usergenic.com/2007/08/27/preserving-with_scope-on-eager-loaded-associations/#comments</comments>
		<pubDate>Mon, 27 Aug 2007 21:22:40 +0000</pubDate>
		<dc:creator>Brendan Baldwin</dc:creator>
		
		<category><![CDATA[ActiveRecord]]></category>

		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.usergenic.com/2007/08/27/preserving-with_scope-on-eager-loaded-associations/</guid>
		<description><![CDATA[(I&#8217;m just gonna start posting things now.  Please excuse the suddenness of this article. &#8211;brendan)

As web developers, one of the first places we turn to when tuning performance of pages that include a lot of data is to minimize the number of queries to the database needed to render a page.  In Rails, [...]]]></description>
			<content:encoded><![CDATA[<p>(I&#8217;m just gonna start posting things now.  Please excuse the suddenness of this article. &#8211;brendan)</p>

<p>As web developers, one of the first places we turn to when tuning performance of pages that include a lot of data is to minimize the number of queries to the database needed to render a page.  In Rails, we use the &#8220;eager loading&#8221; facility provided by the <code>:include</code> option as in <code class="ruby">Article.find(:all, :include =&gt; :author)</code>,  the upshot being that the object represented by the <code>author</code> association is retrieved by the same query that retrieves all of the <code>Article</code> objects.</p>

<p>One of the other niceties of Rails is the ability to apply a scope to restrict the result set for operations conducted on a model within a block, such as (contrived example, just go with me here:)</p>

<pre><code class="ruby">Article.with_scope :find =&gt; {:conditions =&gt; ['published = ?', true]} do
   @author = User.find(user_id)
   @author.articles.each do |article|
     ... show an article or something ...
   end
end
</code></pre>

<p>The problem comes when we combine eager loading with expectations that the scope defined using <code>with_scope</code> is being applied properly:</p>

<pre><code class="ruby">Article.with_scope :find =&gt; {:conditions =&gt; ['published = ?', true]} do
   @author = User.find(user_id, :include =&gt; :articles)
   @author.articles.each do |article|
     ... show an article or something ...
   end
end
</code></pre>

<p>In the above example, the <code class="ruby">:include =&gt; :articles</code> bit eager-loads ALL of the articles into the resulting User object <code>@author</code> and we wind up showing non-published articles in the ensuing loop.  The reason for this is that the sql generated by ActiveRecord does not honor scopes for eager loading.</p>

<p>Here is some code that changes this.</p>

<pre><code class="ruby">ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.class_eval do

  def association_join_with_scoped_eager_loading
    # Don't bother to create the subquery unless we have a finder scope to apply.
    return association_join_without_scoped_eager_loading unless reflection.klass.class_eval{scope :find}
    "#{association_join_without_scoped_eager_loading} AND " &lt;&lt;
    "#{aliased_table_name}.#{reflection.klass.connection.quote_column_name(reflection.klass.primary_key)} IN " &lt;&lt;
    "(#{reflection.klass.__send__(:construct_finder_sql,{:select =&gt; 
      "#{reflection.klass.table_name}.#{reflection.klass.connection.quote_column_name(reflection.klass.primary_key)
    }"})})"
  end

  alias_method_chain :association_join, :scoped_eager_loading

end
</code></pre>

<p>So what&#8217;s going on here?  What we&#8217;re basically doing is creating a subquery that returns all of the ids for the class referenced by the association, to match against the id for the records we are eager-loading, ensuring that we only eager-load associated records that are properly scoped.  The negative performance impact of this approach depends on your database engine and the complexity of the scope you are applying.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.usergenic.com/2007/08/27/preserving-with_scope-on-eager-loaded-associations/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Hello World v2008</title>
		<link>http://www.usergenic.com/2007/08/21/hello-world/</link>
		<comments>http://www.usergenic.com/2007/08/21/hello-world/#comments</comments>
		<pubDate>Tue, 21 Aug 2007 17:16:25 +0000</pubDate>
		<dc:creator>Brendan Baldwin</dc:creator>
		
		<category><![CDATA[Misc.]]></category>

		<guid isPermaLink="false">http://www.usergenic.com/2007/08/21/hello-world/</guid>
		<description><![CDATA[Greetings, programs!

Its been more than half a year since I pulled this site down for renovations and much has happened.  Firstly, I have moved on to another position, this time as lead developer for a web startup, which has resulted in the happy benefit of massive power-ups in my understanding and proficiency with Ruby/Rails. [...]]]></description>
			<content:encoded><![CDATA[<p>Greetings, programs!</p>

<p>Its been more than half a year since I pulled this site down for renovations and much has happened.  Firstly, I have moved on to another position, this time as lead developer for a web <a href="http://www.thepoint.com">startup</a>, which has resulted in the happy benefit of massive power-ups in my understanding and proficiency with <a href="/projects/ruby">Ruby</a>/<a href="/projects/ruby/rails">Rails</a>.  Due to the nature of the software we&#8217;ve been developing, I&#8217;ve had to concoct many extensions for Rails that I&#8217;ve been extracting and maintaining as <a href="/projects/ruby/rails/plugins">plugins</a>, now hosted here at <a href="http://Usergenic.com" title="http://Usergenic.com" target="_blank">Usergenic.com</a>.</p>

<p>I haven&#8217;t had a chance to document them all yet, but this will be coming shortly.  In the meantime, feel free to poke around the <a href="http://usergenic.com/svn/public">public subversion repository</a> and let me know if you find anything useful or have any questions about making it work for you.  I&#8217;ll be documenting and writing some tutorials soon, and will be very interested in incorporating feedback and patches.  If there is enough interest I may migrate the subversion hosting to <a href="http://www.rubyforge.org">Rubyforge</a>.</p>

<p>Anyways, it feels good to be back online and I&#8217;m looking forward to rejoining the conversation with the development community at large.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.usergenic.com/2007/08/21/hello-world/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
