<?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; Ruby on Rails</title>
	<atom:link href="http://www.snowgiraffe.com/tech/tag/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.snowgiraffe.com/tech</link>
	<description>rails, rubies, and sometimes dolphins</description>
	<lastBuildDate>Mon, 17 Oct 2011 17:45:19 +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>8</slash:comments>
		</item>
		<item>
		<title>RailsConf 2009: Integrating SMS with your Rails Application</title>
		<link>http://www.snowgiraffe.com/tech/475/railsconf-2009-integrating-sms-with-your-rails-application/</link>
		<comments>http://www.snowgiraffe.com/tech/475/railsconf-2009-integrating-sms-with-your-rails-application/#comments</comments>
		<pubDate>Thu, 07 May 2009 18:19:59 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ar-extensions]]></category>
		<category><![CDATA[RailsConf]]></category>
		<category><![CDATA[SMS]]></category>
		<category><![CDATA[sms on rails]]></category>

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=475</guid>
		<description><![CDATA[This presentation includes some good tips on how to integrate with sms, especially for Rails applications. The SMS on Rails engine plugin demo steps are included.]]></description>
			<content:encoded><![CDATA[<div id="__ss_1402467" style="width: 425px; text-align: left;"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" title="RailsConf2009 Integrating SMS with your Rails App" href="http://www.slideshare.net/snowgiraffe/railsconf2009-integrating-sms-with-your-rails-app?type=presentation">RailsConf2009 Integrating SMS with your Rails App</a><object width="425" height="355" data="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=railsconf2009sms-090507162144-phpapp01&amp;stripped_title=railsconf2009-integrating-sms-with-your-rails-app" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=railsconf2009sms-090507162144-phpapp01&amp;stripped_title=railsconf2009-integrating-sms-with-your-rails-app" /><param name="allowfullscreen" value="true" /></object></p>
<div style="font-size: 11px; font-family: tahoma,arial; height: 26px; padding-top: 2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/snowgiraffe">Blythe Dunham</a>.</div>
</div>
<p>Some <a href="http://snowgiraffe.com/downloads/presentations/RailsConf2009SMS.ppt">ppt slides</a> for you. Slide share soon to come.</p>
<p><img class="aligncenter size-medium wp-image-476" title="railsyodawg" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/05/railsyodawg-300x192.jpg" alt="railsyodawg" width="300" height="192" /></p>
<h2><a href="http://github.com/blythedunham/smsonrails">SMS On Rails</a><a href="http://snowgiraffe.com/rdocs/sms_on_rails/"> rdoc</a></h2>
<pre>script/plugin install git://github.com/blythedunham/smsonrails</pre>
<p>At the end of the snowgiraffe SMS show, I demoed the <a href="http://github.com/blythedunham/smsonrails">sms on rails engine plugin</a>. I&#8217;ll build it into a gem soon, but by using the generator there shouldn&#8217;t be any collisions, and the plugin should be version safe as everything is namespaced and modularized. Anyhow, here&#8217;s the script:</p>
<pre>&gt; rails dog --database=mysql
&gt; mysqladmin -uroot create dog_development
&gt; cd dog
&gt; script/plugin install git://github.com/blythedunham/smsonrails
&gt; script/generate sms_on_rails setup --default-service-provider=clickatell
&gt; rake db:migrate
&gt; mate config/environment.rb</pre>
<p>Sign up for <a href="http://clickatell.com">clickatell</a> and update the environment.rb with your credentials. Alternatively, use the email gateway (specify no default service provider) and setup your mail settings.</p>
<p>The Generator does the following:</p>
<ol>
<li> copy images and stylesheets to your public directory</li>
<li> generates 3 database migrations (the 2nd and 3rd have a 2 and 3 appended)</li>
<li>installs clickatell gem and static_record_cache plugin (for email carriers caching)</li>
<li>insert configuration into environment.rb</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/475/railsconf-2009-integrating-sms-with-your-rails-application/feed/</wfw:commentRss>
		<slash:comments>7</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>9</slash:comments>
		</item>
		<item>
		<title>MySQL Conf 09: Taking ActiveRecord to the Next Level</title>
		<link>http://www.snowgiraffe.com/tech/446/mysql-conf-09/</link>
		<comments>http://www.snowgiraffe.com/tech/446/mysql-conf-09/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 23:22:01 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ar-extensions]]></category>

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


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

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=311</guid>
		<description><![CDATA[One of the many database features hated by Rails is enumerated columns. Even if you add them to your migration by dirtying your hands with SQL yourself, then the schemadumper interprets them as zero length strings as mentioned in Rob Sanheim&#8217;s blog. Funtime!
I however like enumerated columns because they save space and are pretty, and [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-thumbnail wp-image-310" title="toolbox" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/03/toolbox-150x150.gif" alt="toolbox" width="150" height="150" />One of the many database features hated by Rails is enumerated columns. Even if you add them to your migration by dirtying your hands with SQL yourself, then the <a href="http://caboo.se/doc/classes/ActiveRecord/SchemaDumper.html" target="_blank">schemadumper</a> interprets them as zero length strings as mentioned in <a href="http://robsanheim.com/2006/08/24/rails-schema-dumper-mysql-enums-bad-for-business/" target="_blank">Rob Sanheim&#8217;s blog.</a> Funtime!</p>
<p>I however like enumerated columns because they save space and are pretty, and so I wrote a short and sweet <a href="http://arperftoolkit.rubyforge.org/svn/trunk/migration_enum/init.rb" target="_blank">migration_enum plugin </a> to help me out. However, so be the fate of the web dev that someone else has already written a better one.</p>
<p>The solution is the <a href="http://enum-column.rubyforge.org/">Enumerated Columns</a> by William Sobel. He even added support for radio buttons in your views!</p>
<pre class="ruby"><span class="ident">t</span><span class="punct">.</span><span class="ident">column</span> <span class="symbol">:severity</span><span class="punct">,</span> <span class="symbol">:enum</span><span class="punct">,</span> <span class="symbol">:limit</span> <span class="punct">=&gt;</span> <span class="punct">[</span><span class="symbol">:low</span><span class="punct">,</span> <span class="symbol">:medium</span><span class="punct">,</span> <span class="symbol">:high</span><span class="punct">,</span> <span class="symbol">:critical</span><span class="punct">]</span></pre>
<p>The one thing I dislike is that my &#8220;legacy&#8221; system which has been using enumerated columns interprets them as strings all over the place in the rails code. Enumerated Columns plugins treats them as symbols and breaks me. To fix, I wrote a little <a href="http://snowgiraffe.com/downloads/rails/enum_patch.diff" target="_blank">patch</a>.</p>
<p><span id="more-311"></span></p>
<p>So to get it all:</p>
<pre>script/plugin install svn://rubyforge.org/var/svn/enum-column/plugins/enum-column
cd vendor/plugins/enum-columm
curl http://snowgiraffe.com/downloads/rails/enum_patch.diff &gt; enum_patch.diff
patch -p0 &lt; enum_patch.diff</pre>
<p>In environment.rb (or an initializer) add:</p>
<pre class="ruby"><span class="constant">ActiveRecordEnumerations</span><span class="punct">::</span><span class="constant">Column</span><span class="punct">.</span><span class="ident">use_strings</span> <span class="punct">=</span> <span class="constant">true</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/311/enumeration-columns-with-rails/feed/</wfw:commentRss>
		<slash:comments>1</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>1</slash:comments>
		</item>
		<item>
		<title>Aliasing MySQL Functions with ActiveRecord</title>
		<link>http://www.snowgiraffe.com/tech/117/aliasing-mysql-functions-with-activerecord/</link>
		<comments>http://www.snowgiraffe.com/tech/117/aliasing-mysql-functions-with-activerecord/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 18:20:43 +0000</pubDate>
		<dc:creator>blythe</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[:group]]></category>
		<category><![CDATA[:include]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[aliasing]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[eager loading]]></category>
		<category><![CDATA[GROUP BY]]></category>

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

		<guid isPermaLink="false">http://www.snowgiraffe.com/tech/?p=72</guid>
		<description><![CDATA[Do to some Obama day drama, I wrote a little plugin that allows you to conditionally page or action cache at runtime. This means that you might page cache depending on the user type, or the date or how you feel when you wake up.
script/plugin install http://arperftoolkit.rubyforge.org/svn/trunk/conditional_page_cache
At the top of the controller, indicate which actions [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-79" href="http://www.snowgiraffe.com/tech/?attachment_id=79"><img class="alignright size-thumbnail wp-image-79" title="cash" src="http://www.snowgiraffe.com/tech/wp-content/uploads/2009/01/cash-150x150.jpg" alt="cash" width="104" height="104" /></a>Do to some <a href="http://spongetech.wordpress.com/?p=74">Obama day drama</a>, I wrote a little plugin that allows you to conditionally page or action cache at runtime. This means that you might page cache depending on the user type, or the date or how you feel when you wake up.</p>
<pre style="font-size:10px">script/plugin install http://arperftoolkit.rubyforge.org/svn/trunk/conditional_page_cache</pre>
<p>At the top of the controller, indicate which actions you wish to cache with <span class="inline_code">conditionally_cache</span>.</p>
<pre><span class="keyword">class </span><span class="class">MyController</span> <span class="punct">&lt;</span> <span class="constant">Application</span>

  <span class="ident">conditionally_cache</span> <span class="symbol">:action_one</span><span class="punct">,</span> <span class="symbol">:action_two</span>

  <span class="keyword">def </span><span class="method">cache_page?</span><span class="punct">(</span><span class="ident">current_action_name</span><span class="punct">)</span>
    <span class="attribute">@user</span><span class="punct">.</span><span class="ident">demo?</span>
  <span class="keyword">end</span>

  <span class="keyword">def </span><span class="method">cache_action?</span><span class="punct">(</span><span class="ident">action_name</span><span class="punct">)</span>
    <span class="punct">!</span><span class="attribute">@user</span><span class="punct">.</span><span class="ident">admin?</span>
  <span class="keyword">end</span>

<span class="keyword">end</span></pre>
<p>To use page caching, add a method <span class="inline_code">cache_page?(current_action_name)</span> on the controller.  The page will be cached if this method returns true.</p>
<p>Similarly, return true from method <span class="inline_code">cache_action?(current_action_name)</span> when you want to action cache the page. This method is the same as the one used by the <a href="http://blog.craz8.com/pages/action-cache-plugin">action caching plugin</a> which works great along with this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.snowgiraffe.com/tech/72/conditional-page-cache/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

