<?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; ActiveRecord</title>
	<atom:link href="http://www.snowgiraffe.com/tech/tag/activerecord/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>ar_test_runner: Run the ActiveRecord unit test suite with your modifications</title>
		<link>http://www.snowgiraffe.com/tech/507/ar_test_runner-run-activerecord-unit-tests-with-your-code/</link>
		<comments>http://www.snowgiraffe.com/tech/507/ar_test_runner-run-activerecord-unit-tests-with-your-code/#comments</comments>
		<pubDate>Tue, 12 May 2009 19:43:24 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[ar-extensions]]></category>
		<category><![CDATA[ar_test_runner]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[regression test]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=507</guid>
		<description><![CDATA[ar_test_runner provides a rake task to run the ActiveRecord core test suites with your plugins or files loaded. Make sure ActiveRecord tasks pass with your enhancements.]]></description>
			<content:encoded><![CDATA[<p><a style="padding-right: 5px; padding-bottom:15px;" href="http://www.flickr.com/photos/10407019@N08/868905183/"><img class="size-medium wp-image-517 alignleft" title="ghostrunners" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/05/ghostrunners-300x199.jpg" alt="Run ActiveRecord regression tests with your code loaded" width="216" height="143" /></a>If you write plugins or code to enhance ActiveRecord functionality, its a really good idea to run the ActiveRecord core test suite with your cool additions loaded. After working on <a href="http://github.com/zdennis/ar-extensions">ar-extensions</a>, I liked the idea of running the core tests along with the plugin specific tests.  However, I soon grew tired of coding the same Rakefile for each of my eleventy billion AR plugins. Not DRY. Not fun. Not a unicorn party.</p>
<p>Thus was born, <a href="http://github.com/blythedunham/ar_test_runner">ar_test_runner</a>, a convenient plugin with a rake task to run the ActiveRecord test suites with your  code/gem/plugin load. You even can choose the ActiveRecord version and database.</p>
<h3>Install</h3>
<pre>  gem sources -a http://gems.github.com
  sudo gem install blythedunham-ar_test_runner</pre>
<h3>Run ar_test_runner</h3>
<p>Run all plugins and lib files against sqlite for app version</p>
<pre>ar_test_runner DB=sqlite</pre>
<p>Test 2.2.2 with <a href="http://github.com/blythedunham/rails"><span class="inlineCode">rails_devs_for_data_integrity</span></a> plugin loaded on mysql</p>
<pre>ar_test_runner PLUGIN=rails_devs_for_data_integrity AR_DIR=/Library/Ruby/Gems/1.8/gems/activerecord-2.2.2</pre>
<h3>Rake Tasks</h3>
<p>ar_test_runner works as a rake task, if installed as a plugin or by adding <span class="inlineCode"> require &#8220;ar_test_runner&#8221;</span> to the application&#8217;s Rakefile. The following example tests the applications default version of ActiveRecord with the files <span class="inlineCode">RAILS_ROOT/lib/my_file.rb</span> and <span class="inlineCode">RAILS_ROOT/lib/my_other_file.rb loaded</span></p>
<pre>rake test:activerecord:mysql FILE=lib/my_file.rb,lib/my_other_file.rb</pre>
<h3>Customize</h3>
<p>To specify (or skip)  gems, files, dirs, and plugins ot load or build custom runs see the documentation at <a href="http://github.com/blythedunham/ar_test_runner">ar_test_runner</a> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/507/ar_test_runner-run-activerecord-unit-tests-with-your-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Conf 09: Taking ActiveRecord to the Next Level</title>
		<link>http://www.snowgiraffe.com/tech/446/mysql-conf-09/</link>
		<comments>http://www.snowgiraffe.com/tech/446/mysql-conf-09/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 23:22:01 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ar-extensions]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=446</guid>
		<description><![CDATA[


Today I present Taking Active Record To the Next Level at the MySQL Conference and Expo! There is a lot of excitement in the Rails world due to Rails 3 merge with Merb which will provide framework agnosticism for the people. However, good, old, (sometimes not playing nice with MySQL) ActiveRecord is still the defacto [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mysqlconf.com/mysql2009/public/schedule/detail/7034"><br />
<img style="border: 0px solid; width: 600px; height: 87px;" src="http://assets.en.oreilly.com/1/event/21/mysql2009_728x90.gif" alt="" /><br />
</a></p>
<p><img class="alignleft size-thumbnail wp-image-447" style="margin:10px 10px" title="fat_giraffe" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/04/fat_giraffe-150x150.jpg" alt="fat_giraffe" width="150" height="150" />Today I present <a href="http://www.mysqlconf.com/mysql2009/public/schedule/detail/7034">Taking Active Record To the Next Level</a> at the MySQL Conference and Expo! There is a lot of excitement in the Rails world due to Rails 3 merge with Merb which will provide <a href="http://http://weblog.rubyonrails.org/2008/12/23/merb-gets-merged-into-rails-3">framework agnosticism</a> for the people. However, good, old, (sometimes not playing nice with MySQL) ActiveRecord is still the defacto despite the cool newcomers. Don&#8217;t worry. I bashed my head against the wall with it for 3 years so you don&#8217;t have to. There are lots of tips and tricks for unleashing the power of MySQL and whipping ActiveRecord into enterprise ready shape. Oh, and also included are some really cool pictures like the giraffe on the left.</p>
<div id="__ss_1352250" style="width: 425px; text-align: left;"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" title="MySQLConf2009: Taking ActiveRecord to the Next Level" href="http://www.slideshare.net/snowgiraffe/mysqlconf2009-taking-activerecord-to-the-next-level?type=presentation">MySQLConf2009: Taking ActiveRecord to the Next Level</a><object width="425" height="355" data="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=mysqlconf2009-090427112031-phpapp02&amp;stripped_title=mysqlconf2009-taking-activerecord-to-the-next-level" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=mysqlconf2009-090427112031-phpapp02&amp;stripped_title=mysqlconf2009-taking-activerecord-to-the-next-level" /><param name="allowfullscreen" value="true" /></object></p>
<div style="font-size: 11px; font-family: tahoma,arial; height: 26px; padding-top: 2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/snowgiraffe">Blythe Dunham</a>.<a href="http://snowgiraffe.com/downloads/presentations/MySQLConf2009.ppt"> Download PPT Presentation</a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/446/mysql-conf-09/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>ar-extensions 0.9.1 supports INSERT SELECT for ActiveRecord</title>
		<link>http://www.snowgiraffe.com/tech/418/ar-extensions-activerecordbaseinsert_select/</link>
		<comments>http://www.snowgiraffe.com/tech/418/ar-extensions-activerecordbaseinsert_select/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 21:38:10 +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[insert select]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=418</guid>
		<description><![CDATA[The ar-extensions plugin, which extends ActiveRecord to help optimize interaction between Rails and the database, now provides support for MySQL INSERT SELECT functionality.]]></description>
			<content:encoded><![CDATA[<p>In addition to some other super duper features in version 0.9.1 just released, <a href="http://github.com/zdennis/ar-extensions/tree/master" target="_blank">ar-extensions plugin</a> (and <a href="http://rubyforge.org/frs/?group_id=2113">gem</a>) now supports INSERT SELECT functionality for MySQL to move records from one or more tables into another. Instead of querying eleventy billion records, possibly running out of memory, and then so slowly inserting records one by one, ar-extensions extends ActiveRecord to do it all under the covers in one transaction without writing custom SQL.</p>
<pre class="ruby"><span class="comment"># Insert one copy of each book into the shopping cart. </span>
  <span class="constant">CartItem</span><span class="punct">.</span><span class="ident">insert_select</span><span class="punct">(</span><span class="symbol">:from</span> <span class="punct">=&gt;</span> <span class="symbol">:book</span><span class="punct">,</span>
                         <span class="symbol">:select</span> <span class="punct">=&gt;</span> <span class="punct">['</span><span class="string">books.id, ?, ?, ?, now()</span><span class="punct">',</span> <span class="attribute">@cart</span><span class="punct">.</span><span class="ident">to_param</span><span class="punct">,</span> <span class="number">1</span><span class="punct">,</span> <span class="constant">Time</span><span class="punct">.</span><span class="ident">now</span><span class="punct">],</span>
                         <span class="symbol">:into</span> <span class="punct">=&gt;</span> <span class="punct">[</span><span class="symbol">:book_id</span><span class="punct">,</span> <span class="symbol">:shopping_cart_id</span><span class="punct">,</span> <span class="symbol">:copies</span><span class="punct">,</span> <span class="symbol">:updated_at</span><span class="punct">,</span> <span class="symbol">:created_at</span><span class="punct">]})</span>

<span class="comment"># GENERATED SQL example (MySQL):</span>
<span class="comment"># INSERT INTO `cart_items` ( `book_id`, `shopping_cart_id`, `copies`, `updated_at`, `created_at` )</span>
<span class="comment"># SELECT books.id, '134', 1, '2009-03-02 18:28:25', now() FROM `books`</span></pre>
<p>Using the <span class="inline_code">:keywords</span>, <span class="inline_code">:pre_sql</span>, and <span class="inline_code">:post_sql</span> the <a href="http://snowgiraffe.com/rdocs/ar-extensions/files/ar-extensions/lib/ar-extensions/insert_select_rb.html"> insert_select options</a>, support all <a href="http://dev.mysql.com/doc/refman/5.1/en/insert.html" target="_blank">mysql options</a> available to insert and select statements including</p>
<ul>
<li>IGNORE</li>
<li>ON DUPLICATE KEY UPDATE</li>
<li>LOW_PRIORITY</li>
<li> SQL_CACHE</li>
<li>DELAY</li>
</ul>
<p>before and after comments and anything you can imagine you want to customize.</p>
<p>To install:</p>
<pre><code>script/plugin install git://github.com/zdennis/ar-extensions.git
</code></pre>
<p>For more documentation, refer to <a href="http://snowgiraffe.com/rdocs/ar-extensions/files/ar-extensions/lib/ar-extensions/insert_select_rb.html"> insert_select rdocs</a> and main <a href="http://snowgiraffe.com/rdocs/ar-extensions/">rdocs</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/418/ar-extensions-activerecordbaseinsert_select/feed/</wfw:commentRss>
		<slash:comments>1</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>Introducing &#8230; Static Record Cache plugin</title>
		<link>http://www.snowgiraffe.com/tech/338/introducing-static-record-cache-plugin/</link>
		<comments>http://www.snowgiraffe.com/tech/338/introducing-static-record-cache-plugin/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 22:40:57 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[active record context]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[query cache]]></category>
		<category><![CDATA[static]]></category>
		<category><![CDATA[static record cache]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=338</guid>
		<description><![CDATA[Static Record Cache permanently caches active record data for classes which contain small amounts of static data.]]></description>
			<content:encoded><![CDATA[<p>Static Record Cache permanently caches <span class="inline_code">ActiveRecord</span> data for classes which contain small amounts of static data (data that rarely changes). In addition to caching queries on Ids, methods like <span class="inline_code">find_by_name</span> will use cache hits rather than new queries to retrieve data. Static Record Cache is designed to work with or without the built in <a href="http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2#activerecordcaching" target="_blank"> query cache</a>, but by contrast permanently stores the actual record across all contexts and avoids the overhead of instantiating new <span class="inline_code">ActiveRecord</span> objects.</p>
<h2>Install</h2>
<pre>script/plugin install git://github.com/blythedunham/static_record_cache.git</pre>
<h2>Documentation</h2>
<pre class="ruby"><span class="keyword">class </span><span class="class">SomeStaticClass</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>
  <span class="ident">acts_as_static_record</span>
<span class="keyword">end</span>

<span class="comment">#cache hits</span>
<span class="constant">SomeStaticClass</span><span class="punct">.</span><span class="ident">find_by_name</span><span class="punct">('</span><span class="string">blah</span><span class="punct">')</span>
<span class="constant">SomeStaticClass</span><span class="punct">.</span><span class="ident">find_by_id</span><span class="punct">(</span><span class="number">5</span><span class="punct">)</span>
<span class="constant">SomeStaticClass</span><span class="punct">.</span><span class="ident">find</span> <span class="symbol">:all</span>
<span class="ident">assoc_class</span><span class="punct">.</span><span class="ident">some_static_class</span></pre>
<p>One can specify the cache key for faster lookups and also customize the finder SQL for the class. More documentation can be found in the <a href="http://snowgiraffe.com/rdocs/static_record_cache/">rdocs</a>.</p>
<p><span id="more-338"></span></p>
<p>Another, simpler method of caching can be used by folks using the <a href="http://svn.techno-weenie.net/projects/plugins/active_record_context/README" target="_blank">active record context plugin</a> by technoweenie. Extending <span class="inline_code">StaticRecordCache</span> will permanently cache data for the class regardless of context. This however, only caches by id.</p>
<pre class="ruby"><span class="keyword">class </span><span class="class">TelephoneCarriers</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>
  <span class="ident">extend</span> <span class="constant">StaticActiveRecordContext</span>
<span class="keyword">end</span></pre>
<p>Be sure to install active_record_cache plugin:</p>
<pre>script/plugin install http://svn.techno-weenie.net/projects/plugins/active_record_context/</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/338/introducing-static-record-cache-plugin/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>Aliasing MySQL Functions with ActiveRecord</title>
		<link>http://www.snowgiraffe.com/tech/117/aliasing-mysql-functions-with-activerecord/</link>
		<comments>http://www.snowgiraffe.com/tech/117/aliasing-mysql-functions-with-activerecord/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 18:20:43 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[:group]]></category>
		<category><![CDATA[:include]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[aliasing]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[eager loading]]></category>
		<category><![CDATA[GROUP BY]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=117</guid>
		<description><![CDATA[Changing column data to use MySQL functions instead of the actual column data for Ruby on Rails ActiveRecord is simple. To allow ActiveRecord to retrieve the data of the function,  alias it to the column name.
Event.find :all, :select =&#62; 'substring(name, 1, 10) as name'
This produces the follow SQL query using MySQL SUBSTRING function:
select substring(name, 1, [...]]]></description>
			<content:encoded><![CDATA[<p>Changing column data to use MySQL functions instead of the actual column data for Ruby on Rails ActiveRecord is simple. To allow ActiveRecord to retrieve the data of the function,  alias it to the column name.</p>
<pre><span class="constant">Event</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">substring(name, 1, 10) as name</span><span class="punct">'</span></pre>
<p>This produces the follow SQL query using MySQL <span class="inline_code">SUBSTRING</span> function:</p>
<pre><span class="mysql">select substring(name, 1, 10) as name from events; </span></pre>
<p>ActiveRecord will pull the truncated <span class="inline_code">name</span> (the first 10 characters) in the function data into the ActiveRecord field <span class="inline_code">name</span>. This can be very useful for offloading some work to <a href="http://dev.mysql.com/doc/refman/5.0/en/functions.html" target="_blank">MySQL by using its operations</a> rather than performing the same functions in memory with Rails code.</p>
<p>Aliasing methods can be useful when combined with the <span class="inline_code">GROUP BY </span> functionality. For example, the following query will count the number of events that start on the same day.</p>
<pre><span class="ident">events</span> <span class="punct">=</span> <span class="constant">Event</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">DATE(start_time) as start_time, count(*) as num_events</span><span class="punct">',</span> <span class="symbol">:group</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">DATE(start_time)</span><span class="punct">'</span></pre>
<h2>Notes and Caveats</h2>
<p>Be aware that several issues exist when using MySQL aliasing with ActiveRecord.</p>
<p><span id="more-117"></span></p>
<h3>Eager Loading</h3>
<p>One note is is that ActiveRecord doesn&#8217;t support <span class="inline_code">:select</span> with eager loading. This means that the <span class="inline_code">:select</span> clause is ignored when <span class="inline_code">:include</span> is specified. To work around this problem (and get a performance benefit by selecting fewer columns), install one of the many eager loading plugins referenced on this <a href="http://dev.rubyonrails.org/ticket/5371 ">ticket</a>. Perhaps there are better ones, but I wrote <a href="http://arperftoolkit.rubyforge.org/svn/trunk/eload_select">eload select</a> and have stuck to it.</p>
<pre>script/plugin install http://arperftoolkit.rubyforge.org/svn/trunk/eload_select</pre>
<h3>Aliased Column vs. MySQL Function</h3>
<p>Another caveat is that due to a <a href="http://bugs.mysql.com/bug.php?id=188">MySQL &#8220;Not a Bug&#8221;</a>, the full function <span class="inline_code">DATE(start_time)</span> must be specified instead of just the alias <span class="inline_code">start_time</span>.  This might seem obvious, but the  <span class="inline_code&gt;GROUP BY&lt;/span&gt; clause:&lt;/p&gt; &lt;pre&gt;&lt;span class=&quot;symbol&quot;&gt;:group&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;DATE(start_time)&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&quot;&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;Using the aliased name start_time use the actual column name instead of the aliased function. This would be more intuitive if &lt;span class=">ORDER BY</span> worked the same way. However,  <span class="inline_code">ORDER BY</span> clause does the opposite and uses the <em> function</em> aliased to <span class="inline_code">name</span> instead of the column <span class="inline_code">name</span>. For example,here the data is sorted on the second letter of the title (the substring function).</p>
<pre><span class="constant">Event</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">substring(title, 2) as title</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' /> rder</span> <span class="punct">=&gt;'</span><span class="string">title</span><span class="punct">'</span></pre>
<p>which produces this SQL query:</p>
<pre><span class="mysql">select substring(title, 2) as title from events order by title;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/117/aliasing-mysql-functions-with-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
