<?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; Plugins</title>
	<atom:link href="http://www.snowgiraffe.com/tech/category/ruby-on-rails/plugins/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>Rails Application Health Monitor</title>
		<link>http://www.snowgiraffe.com/tech/599/health-monitor/</link>
		<comments>http://www.snowgiraffe.com/tech/599/health-monitor/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 02:43:54 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[monitor rails health]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=599</guid>
		<description><![CDATA[The health monitor gem allows you to monitor your rails application through a single controller action. In addition to built in database and server environment checks, health monitor allows you to quickly add your customized health checks to the mix.]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="hot-thermometer" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/12/hot-thermometer-172x300.gif" alt="hot-thermometer" width="109" height="191" /><br />
Most rails applications have many additional moving parts of which the health cannot be assessed with simply pinging the (hopefully page cached) homepage</p>
<p>For example,</p>
<ul>
<li>Is Email is sent successfully?</li>
<li>Is the SMS gateway alive and you bought sufficient credits?</li>
<li>All database connections are alive? Backgroundrb down again?</li>
<li>The cloud computing setup jacked the imagemagick? Again?</li>
<li>You are running out of disk space and there are no more file descriptors for your</li>
<li>The git SHA and version is correct</li>
<p style="text-align: center;"><img class="size-medium wp-image-604 aligncenter" title="Screen shot 2009-12-06 at 12.29.53 AM" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/12/Screen-shot-2009-12-06-at-12.29.53-AM-300x100.png" alt="Screen shot 2009-12-06 at 12.29.53 AM" width="300" height="100" /></p>
</ul>
<p><a href="http://github.com/blythedunham/health_monitor" target="_blank">Health Monitor</a> adds a single controller action to generate an html, js, or xml report with details of <strong>all</strong> your custom defined health checks. An error response code (500 server error) indicates failure when any monitored feature fails or exceeds the custom timeout definition. <a href="http://www.snowgiraffe.com/tech/599/health-monitor/" target="_blank">Health monitor</a> is easier to setup than custom server side cron tasks and adds the advantage of exercising more code since everything from your load balancer to nginx to mongrels must be happily shoving rubies around to get that 200 oh boy success message. So ping away, grab a beer and know that hey, you might be too drunk but at least you will know your application is sick before your clients do.</p>
<h2><strong>Install</strong></h2>
<pre>sudo gem install health_monitor --source=http://gemcutter.org

config.gem :health_monitor, :source =&gt; http://gemcutter.org</pre>
<h2><strong>Create a the health monitor controller<br />
</strong></h2>
<pre>script/generate health_monitor</pre>
<h2><strong>Add your customizations</strong></h2>
<pre class="ruby">  <span class="keyword">class </span><span class="class">HealthMonitorController</span> <span class="punct">&lt;</span> <span class="constant">ApplicationController</span>
    <span class="ident">acts_as_health_monitor</span>

    <span class="comment">#More examples available at http://github.com/blythedunham/health_monitor</span>
    <span class="comment">#Built in checks</span>
    <span class="ident">monitor_health</span> <span class="symbol">:schema_check</span><span class="punct">,</span> <span class="symbol">:database_check</span><span class="punct">,</span> <span class="symbol">:ey_agent_check</span>

    <span class="comment">#monitor specified process</span>
    <span class="ident">monitor_process</span> <span class="symbol">:monit</span> 

    <span class="comment"># Custom monitor task</span>
    <span class="ident">monitor_health</span> <span class="symbol">:email</span><span class="punct">,</span> <span class="symbol">:description</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">send an email</span><span class="punct">'</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">controller</span><span class="punct">|</span>
      <span class="constant">ActionMailer</span><span class="punct">::</span><span class="constant">Base</span><span class="punct">.</span><span class="ident">deliver_my_mail</span><span class="punct">('</span><span class="string">blah</span><span class="punct">')</span>
    <span class="keyword">end</span>
  <span class="keyword">end
</span></pre>
<h2>Routes</h2>
<p>Routes are automatically generated:<br />
http://localhost:3000/health_monitor<br />
http://localhost:3000/health_monitor.js</p>
<p>Monitor specific tasks or skip tasks with the parameters <strong>skip</strong> and <strong>only</strong><br />
http://localhost:3000/health_monitor?only=schema_check,database_check<br />
http://localhost:3000/health_monitor?skip=email,database_check</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/599/health-monitor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Delectable plugin treats for the ruby shovers</title>
		<link>http://www.snowgiraffe.com/tech/545/delectable-plugin-treats-for-the-ruby-shovers/</link>
		<comments>http://www.snowgiraffe.com/tech/545/delectable-plugin-treats-for-the-ruby-shovers/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 20:45:37 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ActiveRecord Dumper]]></category>
		<category><![CDATA[AR Dumper]]></category>
		<category><![CDATA[Asset Tag Extensions]]></category>
		<category><![CDATA[Google Base4r]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Ruby Prof]]></category>
		<category><![CDATA[Ruby Prof Request Filters]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=545</guid>
		<description><![CDATA[Been a long time. shouldn&#8217;t of left ya without a dope plugin to step to. meh. But for reals, I&#8217;ve been working way too much. Here&#8217;s a tasty sampler of some new and revived github projects:

ActiveRecord Dumper
Asset Tag Extensions
Ruby Prof Request Filters
Google Base4r extensions

ActiveRecord Dumper
script/plugin install http://github.com/blythedunham/ar_dumper
Dumper. I barely know her! I wrote this ages [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_555" class="wp-caption alignright" style="width: 160px"><img class="size-thumbnail wp-image-555  " style="margin-left: 5px;" title="candy_sampler" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/08/candy_sampler-150x150.jpg" alt="Candy raver plugin funtime!" width="150" height="150" /><p class="wp-caption-text">Delicatable treats for the rubies.</p></div>
<p>Been a long time. shouldn&#8217;t of left ya without a dope plugin to step to. meh. But for reals, I&#8217;ve been working way too much. Here&#8217;s a tasty sampler of some new and revived github projects:</p>
<ul>
<li><a href="http://github.com/blythedunham/asset_tag_extensions">ActiveRecord Dumper</a></li>
<li><a href="http://github.com/blythedunham/asset_tag_extensions">Asset Tag Extensions</a></li>
<li><a href="http://github.com/blythedunham/ruby_prof_request_filters/">Ruby Prof Request Filters</a></li>
<li><a href="http://github.com/blythedunham/base4r/">Google Base4r extensions</a></li>
</ul>
<h2><a href="http://github.com/blythedunham/asset_tag_extensions">ActiveRecord Dumper</a></h2>
<pre>script/plugin install http://github.com/blythedunham/ar_dumper</pre>
<p>Dumper. I barely know her! I wrote this ages ago back in the Rails 1 dot 0h(mg) daze, but still works like a charm.</p>
<p>Easily export records to csv, yaml, or xml using the <tt>:find</tt> options, or <span style="color: #ff0000;">(new) </span>specify an array of activerecords with the <tt>:records</tt> option. AR Dumper dumps to strings or files with a paginated option, and it supports full customization of displayed headers and content with options such as  <tt> <img src='http://www.snowgiraffe.com/tech/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly, :export, :procs, :methods</tt></p>
<pre class="ruby"><span class="constant">Book</span><span class="punct">.</span><span class="ident">dumper</span> <span class="symbol">:yml</span><span class="punct">,</span> <span class="symbol">:find</span> <span class="punct">=&gt;</span> <span class="punct">{</span><span class="symbol">:conditions</span> <span class="punct">=&gt;</span> <span class="punct">['</span><span class="string">author_name like ?</span><span class="punct">',</span> <span class="punct">'</span><span class="string">snowgiraffe</span><span class="punct">']</span> <span class="punct">}</span>

<span class="constant">Book</span><span class="punct">.</span><span class="ident">dumper</span> <span class="symbol">:csv</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' /> nly</span> <span class="punct">=&gt;</span> <span class="punct">[</span><span class="symbol">:author_name</span><span class="punct">,</span> <span class="symbol">:title</span><span class="punct">],</span> <span class="symbol">:records</span> <span class="punct">=&gt;</span> <span class="attribute">@my_books</span></pre>
<p><span class="constant"><br />
</span></p>
<h2><a href="http://github.com/blythedunham/asset_tag_extensions">Asset Tag Extensions</a></h2>
<pre>script/plugin install git://github.com/blythedunham/javascript_tag_extensions</pre>
<p><img class="alignleft size-thumbnail wp-image-556" title="nuts_sampler" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/08/nuts_sampler-150x150.jpg" alt="nuts_sampler" width="136" height="136" />Your high performance web site client loading slowly? In addition to combining asset files, one of the best ways to improve client load speed is to move the javascript files and inline script tags to <a href="http://developer.yahoo.com/performance/rules.html#page-nav">the bottom of the page</a>. Even better, execute inline javascript after the document loads.  To load files at the bottom of the page use the</p>
<pre class="ruby"><span class="ident">include_javascript_tag</span> <span class="punct">'</span><span class="string">myjsfile</span><span class="punct">',</span> <span class="symbol">:defer</span> <span class="punct">=&gt;</span> <span class="constant">true</span></pre>
<p>Similarly, replacing calls to <tt> javascript_tag</tt> with Asset Tag Extension&#8217;s  <tt>inline_javascript</tt> renders the script at the bottom of the page. Use the <tt> <img src='http://www.snowgiraffe.com/tech/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> n_load =&gt; true</tt> functionality use JQuery OnLoad functionality.</p>
<pre class="ruby">  <span class="punct">&lt;%</span>  <span class="ident">inline_javascript</span> <span class="symbol"> <img src='http://www.snowgiraffe.com/tech/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> n_load</span> <span class="punct">=&gt;</span> <span class="constant">true</span> <span class="keyword">do</span> <span class="punct">%&gt;</span><span class="string">
    alert("I love grapes");</span><span class="punct">
  &lt;%</span> end <span class="punct">%&gt;
</span></pre>
<h2><a href="http://github.com/blythedunham/ruby_prof_request_filters/">Ruby Prof Request Filters</a></h2>
<pre>script/plugin install http://github.com/blythedunham/ruby_prof_request_filters</pre>
<p>Need to instantly profile a page? Output <a href="http://ruby-prof.rubyforge.org/">ruby prof</a> results to your browser by adding the following params to the url: <tt>ruby_prof=true</tt> Flat graphs, html graph, whatever you need, this little tool comes in handy when your solving big problems.</p>
<h2><a href="http://github.com/blythedunham/base4r/">Google Base: Base4r</a></h2>
<p>I did some work on Dan Dukeson&#8217;s <a href="http://code.google.com/apis/base/">Google Base API </a>For Ruby. Add, modify and delete items from Google Base (formerly Froogle) with Ruby.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/545/delectable-plugin-treats-for-the-ruby-shovers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails Devs for Data Integrity: How to gracefully handle database key violations</title>
		<link>http://www.snowgiraffe.com/tech/462/rails-devs-for-foreign-keys/</link>
		<comments>http://www.snowgiraffe.com/tech/462/rails-devs-for-foreign-keys/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 02:18:06 +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[foreign keys]]></category>
		<category><![CDATA[Rails Devs for Data Integrity]]></category>
		<category><![CDATA[unique keys]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=462</guid>
		<description><![CDATA[Join the club: Rails Devs for Data Integrity
Some ways to handle unique and foreign key violations database exceptions gracefully in rails.]]></description>
			<content:encoded><![CDATA[<div id="attachment_468" class="wp-caption alignright" style="width: 310px"><img class="size-medium wp-image-468" title="treehouse-fall-04-gif" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/04/treehouse-fall-04-gif-300x197.gif" alt="Join the club! Rails Devs for Data Integrity" width="300" height="197" /><p class="wp-caption-text">Join the club! Rails Devs for Data Integrity</p></div>
<p><span style="color: #ff6600;"><strong>I am a Rails Developer and I believe in data integrity. </strong></span>There I said it! And look, there are lots of people in the club. Jer on Rails had an<a href="http://jeronrails.blogspot.com/2008/01/you-should-use-foreign-key-constraints.html"> awesome post in support for foreign keys</a> a while back.</p>
<p>After my talk on supercharging ActiveRecord to behave in an enterprise environment at the<a href="http://www.snowgiraffe.com/tech/446/mysql-conf-09/"> MySQL conference</a>, a lot of folks were interested in how to get around the Rails <span class="inline_code">ActiveRecord</span> errors that appear when one starts deifying the Rails Way and uses foreign keys and unique indexes on the database. Well I thought about it and came up with not only a list but a yet another new plugin <a href="http://github.com/blythedunham/rails_devs_for_data_integrity" target="_blank">rails_devs_for_data_integrity</a> to help deal with some of the errors. So when someone tells you not to use foreign keys, don&#8217;t listen and<span style="color: #ff6600;"> join the Rails Devs for Data Integrity</span>! Here is how to have em, and still make pretty Rails apps.</p>
<p><span style="color: #ff6600;">Super list&#8230;.</span></p>
<p><span id="more-462"></span></p>
<h2>Unique Keys</h2>
<h3>Use validates_uniqueness_of</h3>
<p>validates_uniqueness_of introduces a query to check unique records before insert or update, but alone doesn&#8217;t guaranty uniqueness in a multi server/multi mongrel environment. By using it conjunction with a unique key on the database, you have proper rails error messages and your safety belt to retain data integrity. In most cases when dealing with a single instance (a single user&#8217;s user name for example), I can afford to eat the overhead of the rails validates_uniqueness_of query.</p>
<h3>Use on duplicate key update and ignore</h3>
<p><a href="http://github.com/zdennis/ar-extensions">ar-extensions 0.9.1</a> supports ON DUPLICATE KEY UPDATE and IGNORE for MySQL on import, save, and create (all inserts and updates). This is especially practical for import (bulk insert) where validating each record&#8217;s uniqueness would produce a lot of overhead.</p>
<h3>Catch and Handle the Duplicate Violation Exception</h3>
<p>I wrote a lot of custom code in models and controllers to catch MySQL duplicate key error (<span class="inline_code">ActiveRecord::StatementInvalid</span>) and handle them appropriately. Typically, I added a new error to ActiveRecord::Base.errors, which is then displayed nicely with <span class="inline_code">error_messages_for</span> in the view. Sometimes this was done often with a save_safe model method possibly aliased to save or a rescue on the controller action.</p>
<p>After MySQLConf I decided to write a little plugin <a href="http://github.com/blythedunham/rails_devs_for_data_integrity">rails_devs_for_data_integrity</a> to convert exceptions into ActiveRecord errors (like validations do) for tables with unique and foreign keys . Its still infantile (and without tests, gasp!) but if anyone likes to find bugs, hit me up on<a href="http://github.com/blythedunham/rails_devs_for_data_integrity/issues"> githubs new issue tracker</a>. If there is more than one unique key per table, you might want write some custom error handling methods.</p>
<pre>script/plugin install git://github.com/blythedunham/rails_devs_for_data_integrity.git</pre>
<pre class="ruby"> <span class="keyword">class </span><span class="class">User</span> <span class="punct">&lt;</span> <span class="constant">ActiveRecord</span><span class="punct">::</span><span class="constant">Base</span>
   <span class="ident">handle_unique_key_violation</span>  <span class="symbol">:user_name</span><span class="punct">,</span> <span class="symbol">:message</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">is taken"
   handle_foreign_key_violation :primary_email_id, :message =&gt; </span><span class="punct">'</span><span class="ident">is</span> <span class="keyword">not</span> <span class="ident">available</span><span class="punct">'</span><span class="string">
 end<span class="normal">

</span></span></pre>
<p>Will write ActiveRecord errors instead of nasty MySQL errors:</p>
<pre> &gt;&gt; user.errors.on(:user_name)
 =&gt; "association does not exist."

 &gt;&gt; user.errors.on(:primary_email_id)
 =&gt; "is a duplicate."</pre>
<h3>Overwrite <em>rescue_action_in_public</em> in ApplicationController</h3>
<p>Its a good idea to either send the user to a pretty 404 static generic error page (fast) or write some custom code in rescue_action_in_public. One idea is to create <span class="inline_code">DisplayableException</span> and subclass any exceptions where the text can be displayed to the user. If this is thrown, show <span class="inline_code">exeception.to_s</span>, if not show a generic error.</p>
<pre class="ruby"><span class="keyword">def </span><span class="method">rescues_action_in_public</span><span class="punct">(</span><span class="ident">exception</span><span class="punct">)</span>
  <span class="attribute">@message</span> <span class="punct">=</span> <span class="keyword">if</span> <span class="ident">exception</span><span class="punct">.</span><span class="ident">is_a?</span><span class="punct">(</span><span class="constant">DisplayableException</span><span class="punct">)</span>
    <span class="ident">exception</span><span class="punct">.</span><span class="ident">to_s</span>
  <span class="keyword">else</span>
    <span class="punct">"</span><span class="string">Sorry but an error occurred. Please contact your mommy.</span><span class="punct">"</span>
  <span class="keyword">end</span>
  <span class="ident">render</span> <span class="symbol">:action</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">error_page</span><span class="punct">'</span>
<span class="keyword">end</span></pre>
<h2>Foreign Keys</h2>
<p>I have been using the <a href="http://agilewebdevelopment.com/plugins/owner/89">Redhills foreign key migration plugin</a> for a long time and haven&#8217;t had too much trouble with foreign key violations.</p>
<h3>Trouble Deleting Records</h3>
<p>At one point <span class="inline_code">ON DELETE</span> was not specified to <span class="inline_code">SET NULL</span> or <span class="inline_code">CASCADE</span> on many dependent columns and there was trouble deleting rows. Reindexing the database with this option was the solution. Similarly,  one could use <span class="inline_code">ON UPDATE</span> on foreign key indexes.  However, due to the way ActiveRecord works and doesn&#8217;t really update id columns, I haven&#8217;t had any issues.</p>
<h3>Foreign Key Validation Errors</h3>
<p>This has happened so seldom that I never worried about it. That&#8217;s what the refresh button is for! Perhaps its because most users have their own data and aren&#8217;t really modifying and deleting simultaneously. However, sure this can happen and it throws an <span class="inline_code">ActiveRecord::StatementInvalid</span> exception as with unique key violations. I would recommend the same approach as for unique keys: catch and handle the exception. I added foreign key support to the <a href="http://github.com/blythedunham/rails_devs_for_data_integrity" target="_blank">rails_devs_for_data_integrity plugin</a> experiment. Yay!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/462/rails-devs-for-foreign-keys/feed/</wfw:commentRss>
		<slash:comments>7</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>
	</channel>
</rss>
