<?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; plugin</title>
	<atom:link href="http://www.snowgiraffe.com/tech/tag/plugin/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>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>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>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>Rails Migrations, MySQL, unsigned integers, primary keys, and a lot of fun times</title>
		<link>http://www.snowgiraffe.com/tech/366/rails-migrations-mysql-unsigned-integers-primary-keys-and-a-lot-of-fun-times/</link>
		<comments>http://www.snowgiraffe.com/tech/366/rails-migrations-mysql-unsigned-integers-primary-keys-and-a-lot-of-fun-times/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 03:34:34 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Migrations]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[:limit]]></category>
		<category><![CDATA[:precision]]></category>
		<category><![CDATA[:scale]]></category>
		<category><![CDATA[column width]]></category>
		<category><![CDATA[mysql_migration_optimizer]]></category>
		<category><![CDATA[numeric type]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[primary key]]></category>
		<category><![CDATA[unsigned]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=366</guid>
		<description><![CDATA[By default Rails uses int(11) as the standard for primary key (id) columns. MySQL prefers unsigned numeric types. Plugin mysql_migration_optimize supports unsigned integers, column display width and complete control over the primary key. Also explained is how to use different numeric types such as tinyint and bigint with Rails Migrations.]]></description>
			<content:encoded><![CDATA[<div id="attachment_383" class="wp-caption alignright" style="width: 298px"><a href="http://www.flickr.com/photos/typefour/2765029015/"><img class="size-medium wp-image-383" title="Antique Keys by Kerri" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/04/antique_keys-288x300.jpg" alt="Customize your primary keys. Pic by Kerri on flickr." width="288" height="300" /></a><p class="wp-caption-text">Customize your primary keys. Pic by Kerri on flickr.</p></div>
<p>Although the standard MySQL type for id columns is an <span class="inline_code">unsigned int</span>, Rails has no support for them and uses regular signed <span class="inline_code">int(11)</span> as the default for primary keys. In addition, most of my tables are less than 16 million rows (max rows  for an unsigned <span class="inline_code">mediumint</span>) and so it saves space to use smaller sized numeric types such as <span class="inline_code">smallint</span> or <span class="inline_code">mediumint</span> to save space.</p>
<p>Inspired by <a href="http://thewebfellas.com/blog/2008/6/2/unsigned-integers-for-mysql-on-rails/">Rob Anderton&#8217;s patch</a> which was dismissed as an uncommon requirement, I built yet another plugin to allow customization of primary keys, support unsigned integers and allow users to specify column width for numeric types.</p>
<pre>script/plugin install http://github.com/blythedunham/mysql_migration_optimizer/tree/master</pre>
<div id="attachment_387" class="wp-caption alignleft" style="width: 160px"><a href="http://www.flickr.com/photos/alexsuarez/2504638107/"><img class="size-thumbnail wp-image-387" title="Ignore this sign" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/04/ignore_sign2-150x150.jpg" alt="Rails ignores signed! pic by alex suarez flikr" width="150" height="150" /></a><p class="wp-caption-text">Rails ignores signs! pic by alex suarez flikr</p></div>
<h2>Specifying Column Width and Unsigned Integers</h2>
<p>The MySQL connection adapter was ignoring the <span class="inline_code">:precision</span> and <span class="inline_code">:scale</span> options and so I just loaded em up to allow unsigned numeric types and allow column width to be specified which was supported on postgresql but not MySQL.</p>
<pre class="ruby"><span class="ident">add_column</span> <span class="symbol">:giraffe</span><span class="punct">,</span> <span class="symbol">:beer_count</span><span class="punct">,</span> <span class="symbol">:integer</span><span class="punct">,</span> <span class="symbol">:limit</span> <span class="punct">=&gt;</span> <span class="number">2</span><span class="punct">,</span> <span class="symbol">:precision</span> <span class="punct">=&gt;</span> <span class="symbol">:unsigned</span><span class="punct">,</span> <span class="symbol">:scale</span> <span class="punct">=&gt;</span> <span class="number">4</span>
#SQL: ALTER TABLE `giraffe` ADD `beer_count` smallint(4) UNSIGNED</pre>
<p><span id="more-366"></span></p>
<ul>
<li>:limit determines the numeric type. Rails introduced smart integer columns in 2.1 which more or less translates to the number of bytes needed for storage. Here&#8217;s a little map taken from the <a href="http://github.com/rails/rails/blob/dc88847e5ce392eed210b97525c14fca55852867/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb">adapter</a> code. Do be careful, however, as some versions of Rails (2.1) <a href="http://blog.smartlogicsolutions.com/2008/06/24/rails-21-broke-my-mysql-foreign-keys/">break foreign keys</a>.<br />
<table style="background-color:#CCCCFF" border="0" align="center">
<tbody>
<tr>
<th> <span class="inline_code">:limit</span></th>
<th> Numeric Type</th>
<th> Column Size</th>
</tr>
<tr>
<td>1</td>
<td><span style="color: #ff9900;">tinyint</span></td>
<td>1 byte</td>
</tr>
<tr>
<td>2</td>
<td><span style="color: #ff9900;">smallint</span></td>
<td>2 bytes</td>
</tr>
<tr>
<td>3</td>
<td><span style="color: #ff9900;">mediumint</span></td>
<td>3 bytes</td>
</tr>
<tr>
<td>nil, 4, 11</td>
<td><span style="color: #ff9900;">int(11)</span></td>
<td>4 bytes</td>
</tr>
<tr>
<td>5 to 8</td>
<td><span style="color: #ff9900;">bigint</span></td>
<td>8 bytes</td>
</tr>
</tbody>
</table>
</li>
<li> :scale: <a href="http://matthom.com/archive/2006/11/28/mysql-integer-columns-and-display-width">column width explained here by Matt Thommes</a> number in parenthesis (4) .</li>
<li>:precision : specify <span class="inline_code">:signed</span> or <span class="inline_code">:unsigned</span>. Default will be <span class="inline_code">:unsigned</span> unless you specify set <span class="inline_code">MySqlMigrationOptimizer.default_sign = :signed </span><br />
in your  enviroment.rb.</li>
</ul>
<h2>Customizing Primary Keys</h2>
<pre class="ruby"><span class="ident">create_table</span> <span class="punct">"</span><span class="string">animal</span><span class="punct">",</span> <span class="symbol">:force</span> <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>  <span class="symbol">:primary_column</span> <span class="punct">=&gt;</span> <span class="punct">{</span><span class="symbol">:type=</span><span class="punct">&gt;</span><span class="symbol">:integer</span><span class="punct">,</span> <span class="symbol">:limit</span> <span class="punct">=&gt;</span> <span class="number">3</span><span class="punct">,</span> <span class="symbol">:scale=</span><span class="punct">&gt;</span><span class="number">5</span><span class="punct">}</span>  <span class="keyword">do</span> <span class="punct">|</span><span class="ident">t</span><span class="punct">|</span> <span class="keyword">end</span>
#SQL: CREATE TABLE `animal` ( `id` mediumint(5) UNSIGNED NOT NULL auto_increment PRIMARY KEY) ENGINE=InnoDB</pre>
<p>If you really want to get crazy you can specify some other type. Here&#8217;s the rope, I&#8217;ll leave the room if you want to hang yourself.</p>
<pre class="ruby"><span class="comment"># Specify a string column as the primary key</span>
<span class="ident">create_table</span> <span class="punct">"</span><span class="string">blah</span><span class="punct">",</span> <span class="symbol">:force</span> <span class="punct">=&gt;</span> <span class="constant">true</span><span class="punct">,</span>
<span class="symbol">:primary_column</span> <span class="punct">=&gt;</span> <span class="punct">{</span><span class="symbol">:type=</span><span class="punct">&gt;</span><span class="symbol">:string</span><span class="punct">,</span> <span class="symbol">:limit=</span><span class="punct">&gt;</span><span class="number">25</span><span class="punct">,</span> <span class="symbol">:auto_increment=</span><span class="punct">&gt;</span><span class="constant">false</span><span class="punct">}</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">t</span><span class="punct">|</span><span class="comment">#</span>
<span class="keyword">end</span>

#SQL: CREATE TABLE `blah` (`id` varchar(25) NOT NULL PRIMARY KEY) ENGINE=InnoDB</pre>
<p>Do be careful to use the same numeric type for the foreign key columns as the referenced id column.</p>
<pre class="ruby"><span class="ident">add_column</span> <span class="symbol"> <img src='http://www.snowgiraffe.com/tech/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> ther_table</span><span class="punct">,</span> <span class="symbol">:blah_id</span><span class="punct">,</span> <span class="symbol">:integer</span><span class="punct">,</span> <span class="symbol">:limit</span> <span class="punct">=&gt;</span> <span class="number">3</span><span class="punct">,</span> <span class="symbol">:scale=</span><span class="punct">&gt;</span><span class="number">5</span></pre>
<p>If you have already generated a lot of tables and do not want to change all the old column types, specify the default to be <span class="inline_code">:signed</span> in config/environment.rb. Any new columns should then specify <span class="inline_code">:precision =&gt; :unsigned</span></p>
<pre class="ruby"><span class="constant">MySqlMigrationOptimizer</span><span class="punct">.</span><span class="ident">default_sign</span> <span class="punct">=</span> <span class="symbol">:signed</span></pre>
<p>The schemadumper has been amended to work correctly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/366/rails-migrations-mysql-unsigned-integers-primary-keys-and-a-lot-of-fun-times/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>Creating Folders Programmatically with Amazon S3&#8217;s API: putting babies in buckets</title>
		<link>http://www.snowgiraffe.com/tech/147/creating-folders-programmatically-with-amazon-s3s-api-putting-babies-in-buckets/</link>
		<comments>http://www.snowgiraffe.com/tech/147/creating-folders-programmatically-with-amazon-s3s-api-putting-babies-in-buckets/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 06:02:47 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[Amazon S3]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[babies]]></category>
		<category><![CDATA[bucket]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[folder]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=147</guid>
		<description><![CDATA[Add folders to your Amazon S3 buckets]]></description>
			<content:encoded><![CDATA[<div id="attachment_165" class="wp-caption alignleft" style="width: 106px"><a rel="attachment wp-att-165" href="http://www.snowgiraffe.com/tech/?attachment_id=165"><img class="size-medium wp-image-165" title="baby_bucket" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/02/baby_bucket-228x300.jpg" alt="Be careful of folder babies in buckets! photo by Patrish McLean" width="96" height="126" /></a><p class="wp-caption-text">Be careful of folder babies in buckets! photo by Patrish McLean</p></div>
<p>Back in the &#8216;burgh at <a href="http://www.wrct.org/" target="_blank">Carnegie Mellon&#8217;s radio station WRCT</a>, a really awesome poster with a baby crawling to its death in a bucket reminded you of how dangerous buckets can really be. Especially when DJing techno.  I can&#8217;t work with <a href="http://aws.amazon.com/s3/" target="_blank">Amazon S3</a> without wishing all my baby resources and folders and files would crawl in to an S3 death bucket and vanish. OMG AMAZON!</p>
<p>So anyhoo, to create folders instead of just files (or what appears to be folders in the awesome <a href="https://addons.mozilla.org/en-US/firefox/addon/3247" target="_blank">S3 firefox organizer</a>), do a <span class="inline_code">PUT</span> with:</p>
<div style='padding:0px 0px 0px 120px'>
<ul>
<li>header <span class="inline_code orange_text">content_type</span> to <span class="inline_code orange_text" >binary/octet-stream</span></li>
<li>append <span class="inline_code orange_text">_$folder$</span> to the end of the path name (as the extension)</li>
<li>set the data to blank</li>
</ul>
</div>
<p>This bitty rails code demonstrates this using the  <a href="http://amazon.rubyforge.org/">S3 plugin</a>:</p>
<p><span id="more-147"></span></p>
<div id="s3rubycode">
<pre>  <span class="constant">S3_FOLDER_EXT</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">_$folder$</span><span class="punct">'</span>
  <span class="keyword">def </span><span class="method">exists_in_S3?</span><span class="punct">(</span><span class="ident">file_name</span><span class="punct">,</span> <span class="ident">bucket_name</span><span class="punct">,</span> <span class="ident">options</span> <span class="punct">={})</span>
    <span class="ident">connect_to_S3</span> 

    <span class="ident">file_search_name</span> <span class="punct">=</span> <span class="ident">file_name</span><span class="punct">.</span><span class="ident">dup</span>
    <span class="ident">file_search_name</span> <span class="punct">&lt;&lt;</span> <span class="constant">S3_FOLDER_EXT</span> <span class="keyword">if</span> <span class="ident">options</span><span class="punct">[</span><span class="symbol">:folder</span><span class="punct">]</span>

    <span class="constant">AWS</span><span class="punct">::</span><span class="constant">S3</span><span class="punct">::</span><span class="constant">Bucket</span><span class="punct">.</span><span class="ident">objects</span><span class="punct">(</span><span class="ident">bucket_name</span><span class="punct">,</span> <span class="symbol">:prefix</span> <span class="punct">=&gt;</span> <span class="ident">file_search_name</span><span class="punct">).</span><span class="ident">any?</span>

  <span class="keyword">end</span>

  <span class="keyword">def </span><span class="method">create_S3_folder</span><span class="punct">(</span><span class="ident">file_name</span><span class="punct">,</span> <span class="ident">bucket_name</span><span class="punct">,</span> <span class="ident">options</span><span class="punct">={})</span>
    <span class="ident">connect_to_S3</span>
    <span class="ident">folder</span> <span class="punct">=</span> <span class="constant">AWS</span><span class="punct">::</span><span class="constant">S3</span><span class="punct">::</span><span class="constant">S3Object</span><span class="punct">.</span><span class="ident">store</span><span class="punct">(</span><span class="ident">file_name</span> <span class="punct">+</span> <span class="constant">S3_FOLDER_EXT</span><span class="punct">,</span> <span class="punct">'</span><span class="punct">',</span> <span class="ident">bucket_name</span><span class="punct">,</span>
      <span class="punct">{</span><span class="symbol">:access</span> <span class="punct">=&gt;</span> <span class="symbol">:public_read</span><span class="punct">,</span>
       <span class="symbol">:content_type</span> <span class="punct">=&gt;</span> <span class="punct">"</span><span class="string">binary/octet-stream</span><span class="punct">",</span>
       <span class="symbol">:cache_control</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">max-age=3600,post-check=900,pre-check=3600</span><span class="punct">'</span> <span class="punct">}.</span><span class="ident">update</span><span class="punct">(</span><span class="ident">options</span><span class="punct">))</span>
  <span class="keyword">end</span>

  <span class="keyword">def </span><span class="method">connect_to_S3</span>
    <span class="keyword">unless</span> <span class="constant">AWS</span><span class="punct">::</span><span class="constant">S3</span><span class="punct">::</span><span class="constant">Base</span><span class="punct">.</span><span class="ident">connected?</span>
      <span class="constant">AWS</span><span class="punct">::</span><span class="constant">S3</span><span class="punct">::</span><span class="constant">Base</span><span class="punct">.</span><span class="ident">establish_connection!</span><span class="punct">(</span><span class="symbol">:access_key_id</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">blah</span><span class="punct">',</span> <span class="symbol">:secret_access_key</span> <span class="punct">=&gt;</span> <span class="punct">'</span><span class="string">giraffe!</span><span class="punct">')</span>
    <span class="keyword">end</span>
  <span class="keyword">end</span></pre>
<p>So your request path would look like:<br />
<span class="inline_code">/baby_bucket?prefix=folder1/folder2_$folder$</span><br />
And the output:<br />
<span class="inline_code"><br />
&lt;ListBucketResult xmlns=&#8221;http://s3.amazonaws.com/doc/2006-03-01/&#8221;&gt;<br />
&lt;Name&gt;baby_bucket&lt;/Name&gt;<br />
&lt;Prefix&gt;folder1/folder2_$folder$&lt;/Prefix&gt;<br />
&lt;Marker&gt;&lt;/Marker&gt;<br />
&lt;MaxKeys&gt;1000&lt;/MaxKeys&gt;<br />
&lt;IsTruncated&gt;false&lt;/IsTruncated&gt;<br />
&lt;/ListBucketResult&gt;<br />
</span></div>
<p>At this point, you should ask yourself why you want to create folders in the first place. Because I want to stick my head in a bucket right now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/147/creating-folders-programmatically-with-amazon-s3s-api-putting-babies-in-buckets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
