<?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>Snow Giraffe Tech &#187; Rails</title>
	<atom:link href="http://www.snowgiraffe.com/tech/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.snowgiraffe.com/tech</link>
	<description>rails, rubies, and sometimes dolphins</description>
	<lastBuildDate>Mon, 07 Jun 2010 14:36:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ActiveRecord on Steroids: Optimize queries using Ar-Extensions 0.9.1</title>
		<link>http://www.snowgiraffe.com/tech/391/activerecord-on-speed-ar-extensions/</link>
		<comments>http://www.snowgiraffe.com/tech/391/activerecord-on-speed-ar-extensions/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 21:42:42 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ar-extensions]]></category>
		<category><![CDATA[:select]]></category>
		<category><![CDATA[database optimization]]></category>
		<category><![CDATA[extensions]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[INSERT]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[save]]></category>
		<category><![CDATA[UPDATE]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=391</guid>
		<description><![CDATA[The ar-extensions plugin extends  ActiveRecord to help developers scale, optimize, and customize Rails interaction with the database. New ar-extensions is the ability to fine tune queries by specifying MySQL database options from ActiveRecord find and save methods.]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-medium wp-image-438" title="legos" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/04/legos-300x216.jpg" alt="legos" width="240" height="173" />The newly released <a href="http://github.com/zdennis/ar-extensions">ar-extensions version 0.9.1</a> includes a handful of new goodies I ported and tweaked and merged from now deprecated <a href="http://arperftoolkit.rubyforge.org/svn/">arperftoolkit</a>, my original toolbox of ActiveRecord tricks we&#8217;ve been using at <a href="http://spongecell.com">Spongecell</a> for over two years. The database agnostic <a href="http://github.com/zdennis/ar-extensions" target="_blank"> ar-extensions plugin</a> and <a href="http://rubyforge.org/frs/?group_id=2113">gem</a> by <a href="http://continuousthinking.com" target="_blank">Zach Dennis</a> extends and enhances the functionality of ActiveRecord to provide developers with a bag of tricks to help scale, optimize, and customize Rails interaction with the database.</p>
<pre>script/plugin install git://github.com/zdennis/ar-extensions.git
gem install ar-extensions</pre>
<p>While the original functionality of database agnostic <a href="http://github.com/zdennis/ar-extensions/tree/master">ar-extensions</a> is to import (bulk insert) many records quickly, my favorite new features for 0.9.1 are the <span style="color: #ff6600;">new find and save options</span> which allow developers to quickly customize queries without writing SQL for the MySQL database.</p>
<h2>Find</h2>
<p>For instance, suppose the wrong index is used to perform your favorite query. This use to mean that the developer had to dig through the logs, find the generated query, and rewrite the entire query with the correct index in SQL and execute it with <span class="inline_code">ActiveRecord::Base.find_by_sql</span>. With ar-extensions, its just another parameter to the find method. Similarly, leveraging MySQL functionality such as <span class="inline_code">SQL_CACHE</span>, <span class="inline_code">HIGH PRIORITY</span>, and <span class="inline_code">LOCK IN SHARED MODE</span> is just as easy.</p>
<pre class="ruby"><span class="constant">Beer</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:index_hint</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">USE INDEX (uk_beer_name)</span><span class="punct">'
#SQL: SELECT * from `beers` USE INDEX (uk_beer_name)
</span>
<span class="constant">Beer</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:keywords</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string"> SQL_CACHE HIGH_PRIORITY</span><span class="punct">'
#SQL: </span><span class="punct">SELECT SQL_CACHE HIGH_PRIORITY * from `beers` </span><span class="punct"> </span>

<span class="constant">Beer</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:post_sql</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string"> FOR UPDATE</span><span class="punct">',</span> <span class="symbol">:pre_sql</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">/* My little comment*/</span><span class="punct">'</span>
<span class="comment">#SQL: /* My little comment*/ SELECT * from `beers` FOR UPDATE </span></pre>
<h2>Finder SQL Exposed</h2>
<p>And if that&#8217;s not enough to take advantage of<a href="http://dev.mysql.com/doc/refman/5.1/en/select.html" target="_blank"> MySQL query options</a>, <span class="inline_code">finder_sql_to_string</span> will send the query string back and you can gsub your heart out before execution. It&#8217;s also a great to use for debugging purposes.</p>
<pre class="ruby"><span class="ident">sql</span> <span class="punct">=</span> <span class="constant">Beer</span><span class="punct">.</span><span class="ident">send</span> <span class="symbol">:finder_sql_to_string</span><span class="punct">,</span> <span class="symbol">:conditions</span> <span class="punct">=&gt;</span> <span class="punct">['</span><span class="string">flavor like ?</span><span class="punct">',</span> <span class="punct">'</span><span class="string">delicious</span><span class="punct">']</span>
<span class="constant">Beer</span><span class="punct">.</span><span class="ident">find_by_sql</span> <span class="ident">sql</span><span class="punct">.</span><span class="ident">gsub</span><span class="punct">('</span><span class="string">WHERE</span><span class="punct">',</span> <span class="punct">'</span><span class="string">where /* I heart lowercase */</span><span class="punct">')</span></pre>
<h2>Save and Create</h2>
<p>As for save and create, the new options provide more control over database <a href="http://dev.mysql.com/doc/refman/5.1/en/insert.html" target="_blank">inserts</a> and <a href="http://dev.mysql.com/doc/refman/5.1/en/update.html">updates</a>, especially when unique keys are involved.</p>
<pre class="ruby"><span class="constant">Animal</span><span class="punct">.</span><span class="ident">create!</span><span class="punct">({</span><span class="symbol">:name</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">Jerry Giraffe</span><span class="punct">'},</span> <span class="symbol"> <img src='http://www.snowgiraffe.com/tech/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> n_duplicate_key_update</span> <span class="punct">=&gt;</span> <span class="punct">[</span><span class="symbol">:password</span><span class="punct">])</span>
<span class="comment">#INSERT INTO animals (`name`, `fav_beer`, `password`) VALUES('Jerry Giraffe', 'Pabst', NULL) ON DUPLICATE KEY UPDATE `animals`.`password`=VALUES(`password`)</span>

<span class="ident">animal</span><span class="punct">.</span><span class="ident">save</span><span class="punct">(</span><span class="symbol">:keywords</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">LOW_PRIORITY</span><span class="punct">',</span> <span class="symbol">:ignore</span> <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>
            <span class="symbol">:pre_sql</span> <span class="punct">=&gt;</span> <span class="punct">"</span><span class="string">/* Now I know where this query is coming from within my Rails code!*/</span><span class="punct">")</span>
<span class="comment">#/* Now I know where this query is coming from within my Rails code!*/ UPDATE LOW_PRIORITY IGNORE `animals` SET `fav_beer` = 'Pabst', `password` = 'frenchfry', `name` = 'Party Giraffe' WHERE `id` = 1</span></pre>
<p>In addition to these spiffy new features, <a href="http://github.com/zdennis/ar-extensions/tree/master">ar-extensions</a> provides support for all of these:</p>
<ul>
<li> <span style="color: #ff0000;">(new)</span> <a href="http://www.snowgiraffe.com/tech/391/activerecord-o…ensions-pluginactiverecord-on-speed-new-tools-for-the-ar-extensions-plugin/" target="_blank">create and save options</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/files/ar-extensions/lib/ar-extensions/create_and_update_rb.html" target="_blank">rdoc</a></li>
<li> <span style="color: #ff0000;">(new)</span> <a href="http://www.snowgiraffe.com/tech/391/activerecord-o…ensions-pluginactiverecord-on-speed-new-tools-for-the-ar-extensions-plugin/" target="_blank">find options</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/files/ar-extensions/lib/ar-extensions/finder_options_rb.html" target="_blank">rdoc</a></li>
<li> <span style="color: #ff0000;">(new)</span> <a href="http://www.snowgiraffe.com/tech/418/ar-extensions-activerecordbaseinsert_select/">insert_select</a> from one table to another <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Base.html#M000048" target="_blank">rdoc</a></li>
<li> <span style="color: #ff0000;">(new) </span>find and count unions <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Base.html#M000053" target="_blank">rdoc</a></li>
<li> <span style="color: #ff0000;">(new)</span> delete options <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Base.html#M000038" target="_blank">rdoc</a></li>
<li> <a title="Import" href="http://www.continuousthinking.com/2007/5/6/activerecord-extensions-0-6-0-released" target="_blank">import (bulk insert) with synchronization</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Base.html#M000044" target="_blank">rdoc</a></li>
<li> <a href="http://www.continuousthinking.com/2007/1/31/activerecord-temporary-tables-and-merging-mysql" target="_blank">csv export</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Extensions/FindToCSV.html" target="_blank">rdoc</a></li>
<li> <a title="Better Finder Hash Support" href="http://www.continuousthinking.com/2007/3/14/activerecord-extensions-0-5-0-released" target="_blank">better finder hash support</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Extensions.html" target="_blank">rdoc</a></li>
<li> <a title="Disabling Foreign Keys" href="http://www.continuousthinking.com/2007/1/31/activerecord-temporary-tables-and-merging-mysql" target="_blank">disabling foreign keys</a> <a href="http://snowgiraffe.com/rdocs/ar-extensions/files/ar-extensions/lib/ar-extensions/foreign_keys_rb.html" target="_blank">rdoc</a></li>
<li>fulltext search support <a href="http://snowgiraffe.com/rdocs/ar-extensions/classes/ActiveRecord/Extensions/FullTextSearching.html" target="_blank">rdoc</a></li>
<li><a title="Temporary Table Nicety" href="http://www.continuousthinking.com/2007/1/31/activerecord-temporary-tables-and-merging-mysql" target="_blank">temporary table manipulation</a> <a title="Temporary Table rdoc" href="file:///Users/blythie/radrails/ar_test/ar_test/vendor/plugins/ar-extensions/ar-extensions/doc/classes/ActiveRecord/Base.html#M000046" target="_blank"> rdoc</a></li>
</ul>
<p>That&#8217;s a lot of rope! If you do by chance hang yourself, please send bugs, comments and even patches to the <a title="Github issue tracker for Ar-Extensions" href="http://github.com/zdennis/ar-extensions/issues" target="_blank">new github issue tracker</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/391/activerecord-on-speed-ar-extensions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When to :select and :include your rubies and rails</title>
		<link>http://www.snowgiraffe.com/tech/327/when-to-select-and-include-your-rubies-and-rails/</link>
		<comments>http://www.snowgiraffe.com/tech/327/when-to-select-and-include-your-rubies-and-rails/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 21:16:41 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[:include]]></category>
		<category><![CDATA[:select]]></category>
		<category><![CDATA[ActiveRecordContext]]></category>
		<category><![CDATA[eager loading]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=327</guid>
		<description><![CDATA[Although plugins exist to force :select and :include to play nice with ActiveRecord::Base.find, using this approach does not always yield the best performance. The post discusses when to use eager loading and when to use other approaches and the plugins that will help you.]]></description>
			<content:encoded><![CDATA[<div id="attachment_354" class="wp-caption alignleft" style="width: 160px"><img class="size-thumbnail wp-image-354" title="Finger Monkey" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/04/mini-monkey-3-150x150.jpg" alt="Every blog post could use a finger monkey" width="150" height="150" /><p class="wp-caption-text">Every blog post could use a finger monkey</p></div>
<p>The vanilla rails ActiveRecord finders do not allow us to specify the <span class="inline_code">:select</span> clause when associations are eager loaded with the <span class="inline_code">:include</span> option. There has been <a href="http://dev.rubyonrails.org/ticket/5371">ticket</a> after <a href="http://dev.rubyonrails.org/attachment/ticket/7147/options_select_working_with_eager_loading.diff" target="_blank">ticket</a> on the rails site the proposed  patches were rejected on the grounds that an alternative, such as <a href="http://svn.techno-weenie.net/projects/plugins/active_record_context/README">ActiveRecordContext</a> (a fantasic plugin by the way) should be used instead.<br />
From the database perspective, selecting fewer columns can give  huge performance boosts in some situations especially when the selected fields are indexed. However, often when joined tables are sparse (many base table records are pointing to the same joined table records), you might be better off running two queries: one on the base table, followed by a second on the join table with the collected foreign keys. To illustrate this, an employee has a fairly unique address while she shares her position in the company with several others.</p>
<pre class="ruby"><span class="constant">Employee</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:include</span> <span class="punct">=&gt;</span> <span class="symbol">:position</span></pre>
<p>would probably be less efficient than</p>
<pre class="ruby"><span class="ident">employees</span> <span class="punct">=</span> <span class="constant">Employee</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span>

<span class="ident">positions</span> <span class="punct">=</span> <span class="constant">Positions</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:conditions</span> <span class="punct">=&gt;</span> <span class="punct">['</span><span class="string">id in (?)</span><span class="punct">',</span> <span class="ident">employees</span><span class="punct">.</span><span class="ident">collect</span><span class="punct">(&amp;</span><span class="symbol">:id</span><span class="punct">)]</span>

<span class="ident">employees</span><span class="punct">.</span><span class="ident">each</span><span class="punct">{|</span><span class="ident">employee</span><span class="punct">|</span> <span class="ident">employee</span><span class="punct">.</span><span class="ident">position</span> <span class="punct">=</span> <span class="ident">positions</span><span class="punct">.</span><span class="ident">select</span> <span class="punct">{|</span><span class="ident">p</span><span class="punct">|</span> <span class="ident">p</span><span class="punct">.</span><span class="ident">to_param</span> <span class="punct">==</span> <span class="ident">employee</span><span class="punct">.</span><span class="ident">posistion_id</span><span class="punct">}</span></pre>
<p>This is exactly what <a href="http://svn.techno-weenie.net/projects/plugins/active_record_context/README">active record context </a>does but without the messy details. In addition, the records are cached so any subsequent references to the associations hit the cache instead of rerunning a query.</p>
<p><span id="more-327"></span></p>
<pre class="ruby"><span class="ident">employees</span> <span class="punct">=</span> <span class="constant">Employee</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span>
<span class="constant">Positions</span><span class="punct">.</span><span class="ident">preload</span><span class="punct">(</span><span class="ident">employees</span><span class="punct">)</span></pre>
<p>On the other hand, provided that a reasonable number of records are queried, including the address would probably be more efficient.</p>
<pre class="ruby"><span class="constant">Employee</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:include</span> <span class="punct">=&gt;</span> <span class="symbol">:address</span><span class="punct">,</span> <span class="symbol">:select</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">addresses.city, employees.*</span><span class="punct">'</span></pre>
<p>In this case wouldn&#8217;t it be great to use a <span class="inline_code">:select</span> clause if for instance you were only interested in for instance the city. To achieve this, I updated the <a href="http://www.snowgiraffe.com/tech/?p=329" target="_self">eload select plugin</a>. It does use some tricky parsing but does have the advantage of accepting aliases and allowing you to select from the base table (some alternates include all base columns).</p>
<p>One disadvantage of using <span class="inline_code">:select</span> is a lot of overhead is spent in rails on aliasing columns. Its really nasty in there and probably the reason that <span class="inline_code">:select</span> is not supported with <span class="inline_code">:include</span> out of the box.</p>
<p>If you really are having performance issues, its probably best to abandon the <span class="inline_code">:include</span> altogether and rewrite it as a :join.</p>
<pre class="ruby"><span class="ident">employees</span> <span class="punct">=</span> <span class="constant">Employee</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:select</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">addresses.city as address_city, employees.*</span><span class="punct">',</span>

<span class="symbol">:joins</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">left outer join addresses on addresses.id = employees.address_id</span><span class="punct">'</span>

<span class="ident">employees</span><span class="punct">.</span><span class="ident">first</span><span class="punct">['</span><span class="string">address_city</span><span class="punct">']</span></pre>
<p><img class="alignright size-thumbnail wp-image-356" title="piggy" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/04/piggy-150x150.jpg" alt="piggy" width="150" height="150" />Stefan Kaes has written an excellent plugin called <a title="piggy back plugin" href="http://railsexpress.de/svn/plugins/piggy_back/trunk/README" target="_blank">piggy back</a> that does this for you with <span class="inline_code">belongs_to</span> and <span class="inline_code">has_one</span> relationships. For common delegated fields, this plugin is a winner.</p>
<pre class="ruby"><span class="constant">Employees</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:piggy</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">address_city</span><span class="punct">'</span>

<span class="ident">employees</span><span class="punct">.</span><span class="ident">first</span><span class="punct">.</span><span class="ident">address_city</span></pre>
<p>So in conclusion, install <a href="http://www.snowgiraffe.com/tech/?p=329">eload-select plugin </a>and you get <span class="inline_code">:select</span> and <span class="inline_code">:include</span> playing happily together. However, sometimes its not the best option out there. There is a huge open source toolbox. Use them wisely.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/327/when-to-select-and-include-your-rubies-and-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eager loading Select Plugin: when :select plays nice with :include</title>
		<link>http://www.snowgiraffe.com/tech/329/eager-loading-select-plugin-when-select-plays-nice-with-include/</link>
		<comments>http://www.snowgiraffe.com/tech/329/eager-loading-select-plugin-when-select-plays-nice-with-include/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 15:50:26 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[:include]]></category>
		<category><![CDATA[:select]]></category>
		<category><![CDATA[eager loading]]></category>
		<category><![CDATA[eload-select]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=329</guid>
		<description><![CDATA[eload-select plugin. Allowing :select to play nice with :include for Ruby on Rails ActiveRecord finders.]]></description>
			<content:encoded><![CDATA[<p>With <span class="inline_code">ActiveRecord::Base.find</span>, <span class="inline_code">:select</span> is ignored when <span class="inline_code">:include</span> is specified. There&#8217;s been many a <a href="http://dev.rubyonrails.org/ticket/5371" target="_blank">ticket</a> and proposed but denied <a href="http://dev.rubyonrails.org/attachment/ticket/7147/options_select_working_with_eager_loading.diff" target="_blank">patch</a> and <a href="http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/7b5b6f40b9735723/e69da924283f7505" target="_blank">google discussion</a> to provide this functionality.</p>
<p>eload-select plugin to the rescue!</p>
<pre class="ruby"><span class="constant">Employee</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:select</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">addresses.city, address.state, employees.*</span><span class="punct">',</span>
<span class="symbol">                    :include</span> <span class="punct">=&gt;</span> <span class="symbol">:address</span></pre>
<p>The advantages of the <a href="http://github.com/blythedunham/eload-select/tree/master" target="_blank">eload-select plugin</a>:</p>
<ol>
<li>Works with aliases and database functions</li>
<pre class="ruby"><span class="constant">Employee</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:first</span><span class="punct">,</span>
  <span class="symbol">:select</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">now() as current_time, addresses.city, DATE(addresses.created_at) as addresses.created_at, employee.*</span><span class="punct">'</span>
  <span class="symbol">:include</span> <span class="punct">=&gt;</span> <span class="symbol">:address</span></pre>
<li>Plays nice with other plugins like <a href="http://www.continuousthinking.com/tags/arext" target="_blank">ar-extensions</a></li>
<li>Selects columns on the base table (some options force you to select all of the base table)</li>
<pre class="ruby"><span class="constant">Employee</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span><span class="punct">,</span> <span class="symbol">:select</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">addresses.city, employees.name, employees.start_date</span><span class="punct">',</span>
                    <span class="symbol">:include</span> <span class="punct">=&gt;</span> <span class="symbol">:address</span></pre>
</ol>
<pre>script/plugin install git://github.com/blythedunham/eload-select.git</pre>
<p>And your off! Recently updated to work with all versions of rails including 2.3.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/329/eager-loading-select-plugin-when-select-plays-nice-with-include/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MULTIMONGREL! Awesome Action Caching with Rails</title>
		<link>http://www.snowgiraffe.com/tech/9/multimongrel-awesome-action-caching-with-rails/</link>
		<comments>http://www.snowgiraffe.com/tech/9/multimongrel-awesome-action-caching-with-rails/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 23:00:10 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[Caching]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[action cache]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[mongrel]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[X-Accel-Redirect]]></category>
		<category><![CDATA[X-SendFile]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=9</guid>
		<description><![CDATA[Action Cache Plugin is super. Tweak it a little for use with multi mongrel environment!]]></description>
			<content:encoded><![CDATA[<div id="attachment_20" class="wp-caption alignright" style="width: 310px"><img class="size-medium wp-image-20" title="multiball1" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/01/multiball1-300x197.jpg" alt="playing pinball in Vegas reminded me of mongrel multi caching funtime" width="300" height="197" /><p class="wp-caption-text">playing pinball in Vegas reminded me of mongrel multi caching funtime</p></div>
<p>We really like cache with our rails at <a href="http://spongecell.com">Spongecell</a> and have been really successful with <a href="http://blog.craz8.com/pages/action-cache-plugin" target="_blank">Tom Fake&#8217;s Action Cache plugin</a> when we are unable to page cache highly demanded content.</p>
<p>One little concurrency issue that I encountered running with the Action cache plugin on multiple mongrels was one mongrel would delete (expire) a meta data file while another was simultaneously writing the meta or data file. Anyhoo, the result was an angry corrupt file that was served to other web clients. This made some of customers angry. Then I was angry. To avoid all the hatred, I patched the method <em>cached_entry</em> in <em>action_cache.rb</em> with this little change to remove the cache on exceptions. <a href="http://snowgiraffe.com/downloads/rails/action_cache.diff"><span style="color: #339966;">action_cache.diff</span></a></p>
<pre>        rescue
          remove_cache_item(controller)
          return nil
        end</pre>
<p>With that said, here are my favorite features which you will love too!<br />
<span id="more-9"></span></p>
<ul>
<li>The timer expiry. So&#8230; you can be super lazy and just expire your cache say every 10 minutes without having to write complex sweeping code that may or may not slow down some other actions.<br />
<code>@response.time_to_live = 10.minutes</code></li>
</ul>
<ul>
<li>Easy integration with  <a href="http://spongetech.wordpress.com/2007/11/13/the-complete-nginx-solution-to-sending-flowers-and-files-with-rails/">Nginx X-Accel-Redirect </a> (and Apache&#8217;s X-SendFile) to tell your web server to send the cached file downstream. This allows you to still use before and after filters, but  offloads the work the Nginx and free up your mongrels a bit to bark at someone else.</li>
</ul>
<p>For more information on rails caching, check out <a href="http://http://www.railsenvy.com/2007/2/28/rails-caching-tutorial">Gregg from Rail Envy&#8217;s</a> sweet post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/9/multimongrel-awesome-action-caching-with-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
