<?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>Eigenjoy &#187; rails</title>
	<atom:link href="http://eigenjoy.com/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://eigenjoy.com</link>
	<description>a programming blog</description>
	<lastBuildDate>Wed, 14 Dec 2011 18:22:45 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>activeuuid &#8211; binary uuid primary keys in Rails 3.1 on MySQL</title>
		<link>http://eigenjoy.com/2011/08/29/activeuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql/</link>
		<comments>http://eigenjoy.com/2011/08/29/activeuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 14:42:38 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/?p=494</guid>
		<description><![CDATA[I&#8217;ve been debating a lot recently if I want to use NoSQL (Cassandra) for my next project or if I should just use MySQL. I plan to write a longer post on this soon, but for now I want to share with you a little gem I wrote.
I plan to use NoSQL in the not-too-distance [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been debating a lot recently if I want to use NoSQL (Cassandra) for my next project or if I should just use MySQL. I plan to write a longer post on this soon, but for now I want to share with you a little gem I wrote.</p>
<p>I plan to use NoSQL in the not-too-distance future, but not today. When I port the data over I don&#8217;t want to have to map &#8220;old ids&#8221; to &#8220;new ids&#8221;. By using a <tt>uuid</tt> from day one I can easily maintain referential integrity when I migrate to a new datastore.</p>
<p>I looked up how this is typically done in Rails using MySQL and all I could find was examples that store the UUID as strings (e.g. <a href="https://gist.github.com/937739">here</a>). The UUID is natively just 16 bytes so storing this in a <tt>VARCHAR(36)</tt> seemed like a poor solution. Ideally we would store the UUID as binary and let the ORM deal with translating it to and from a UUID object.</p>
<p><tt>activeuuid</tt> solves this problem. <tt>activeuuid</tt> is a Rails plugin that will let you use <tt>UUIDTools::UUID</tt> objects as primary keys for your ActiveRecords and it stores them in MySQL as <tt>binary(16)</tt>.</p>
<p>Here&#8217;s how to use it:</p>
<h2>Create a Migration</h2>
<p><tt>activeuuid</tt> adds the <tt>uuid</tt> type to your migrations. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateEmails <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:emails</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>  <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#9900CC;">uuid</span> <span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:unique</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
      t.<span style="color:#9900CC;">uuid</span> <span style="color:#ff3333; font-weight:bold;">:sender_id</span>  <span style="color:#008000; font-style:italic;"># belongs_to :sender</span>
&nbsp;
      t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:subject</span>
      t.<span style="color:#9900CC;">text</span> <span style="color:#ff3333; font-weight:bold;">:body</span>
&nbsp;
      t.<span style="color:#9900CC;">timestamp</span> <span style="color:#ff3333; font-weight:bold;">:sent_at</span>
      t.<span style="color:#9900CC;">timestamps</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    add_index <span style="color:#ff3333; font-weight:bold;">:emails</span>, <span style="color:#ff3333; font-weight:bold;">:id</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:emails</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h2>include ActiveUUID::UUID in your model</h2>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Email <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveUUID::UUID</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:sender</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h2>use it</h2>
<p>Here are some example specs:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'spec_helper'</span>
&nbsp;
describe Email <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
  context <span style="color:#996600;">&quot;when using uuid's as keys&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      Email.<span style="color:#9900CC;">delete_all</span>
      <span style="color:#0066ff; font-weight:bold;">@guid</span> = <span style="color:#996600;">&quot;1dd74dd0-d116-11e0-99c7-5ac5d975667e&quot;</span>
      <span style="color:#0066ff; font-weight:bold;">@e</span> = Email.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:subject</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;hello&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;world&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> e.<span style="color:#9900CC;">id</span> = <span style="color:#6666ff; font-weight:bold;">UUIDTools::UUID</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>@guid<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#0066ff; font-weight:bold;">@e</span>.<span style="color:#9900CC;">save</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    it <span style="color:#996600;">&quot;the id guid should be equal to the uuid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@e</span>.<span style="color:#9900CC;">id</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">should</span> eql<span style="color:#006600; font-weight:bold;">&#40;</span>@guid<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    it <span style="color:#996600;">&quot;should be able to find an email by the uuid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      f = Email.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">UUIDTools::UUID</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>@guid<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      f.<span style="color:#9900CC;">id</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">should</span> eql<span style="color:#006600; font-weight:bold;">&#40;</span>@guid<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h2>Benefits</h2>
<p>There are a number of benefits to using UUIDs as primary keys.:</p>
<ul>
<li>no id conflict during multi-master write</li>
<li>no locking due to auto-increment</li>
<li>with time-based UUIDs you can store a timestamp within your UUID</li>
<li>you can create natural keys (based on the SHA of model attributes)</li>
</ul>
<h2>Future work</h2>
<ul>
<li>more transparent support for natural and composite keys</li>
<li>support for MySQLs `INSERT &#8230; ON DUPLICATE KEY UPDATE` syntax</li>
<li>support a primary column name other than `id`</li>
<li> work on other databases (Postgres, etc)</li>
<li>tests</li>
</ul>
<h2>Installation</h2>
<p>Add this to your <tt>Gemfile</tt></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    gem <span style="color:#996600;">&quot;activeuuid&quot;</span></pre></div></div>

<p>Or get the code here: <a href="https://github.com/jashmenn/activeuuid">https://github.com/jashmenn/activeuuid</a></p>
<h2>References</h2>
<ul>
<li><a href="http://bret.appspot.com/entry/how-friendfeed-uses-mysql">http://bret.appspot.com/entry/how-friendfeed-uses-mysql</a></li>
<li><a href="http://kekoav.com/blog/36-computers/58-uuids-as-primary-keys-in-mysql.html ">http://kekoav.com/blog/36-computers/58-uuids-as-primary-keys-in-mysql.html</a></li>
<li><a href="https://gist.github.com/937739">https://gist.github.com/937739</a></li>
<li><a href="http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html">http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html</a></li>
<li><a href="http://krow.livejournal.com/497839.html">http://krow.livejournal.com/497839.html</a></li>
<li><a href="https://github.com/jamesgolick/friendly">https://github.com/jamesgolick/friendly</a></li>
</ul>
<h2>Dependencies</h2>
<p>Note that this depends on Rails 3.1 because it uses the custom column serialization added by Aaron Patterson. (See: <a href="https://github.com/rails/rails/commit/ebe485fd8ec80a1a9b86516bc6f74bc5bbba3476">https://github.com/rails/rails/commit/ebe485fd8ec80a1a9b86516bc6f74bc5bbba3476</a>)</p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;title=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL&amp;notes=I%27ve%20been%20debating%20a%20lot%20recently%20if%20I%20want%20to%20use%20NoSQL%20%28Cassandra%29%20for%20my%20next%20project%20or%20if%20I%20should%20just%20use%20MySQL.%20I%20plan%20to%20write%20a%20longer%20post%20on%20this%20soon%2C%20but%20for%20now%20I%20want%20to%20share%20with%20you%20a%20little%20gem%20I%20wrote.%0D%0A%0D%0AI%20plan%20to%20use%20NoSQL%20in%20t" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;title=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL%20-%20http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;t=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;title=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL&amp;annotation=I%27ve%20been%20debating%20a%20lot%20recently%20if%20I%20want%20to%20use%20NoSQL%20%28Cassandra%29%20for%20my%20next%20project%20or%20if%20I%20should%20just%20use%20MySQL.%20I%20plan%20to%20write%20a%20longer%20post%20on%20this%20soon%2C%20but%20for%20now%20I%20want%20to%20share%20with%20you%20a%20little%20gem%20I%20wrote.%0D%0A%0D%0AI%20plan%20to%20use%20NoSQL%20in%20t" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;t=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2011/08/29/activeuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>visualize state_machine&#8217;s with graphviz</title>
		<link>http://eigenjoy.com/2008/10/14/visualize-state_machines-with-graphviz/</link>
		<comments>http://eigenjoy.com/2008/10/14/visualize-state_machines-with-graphviz/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 16:04:23 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2008/10/14/visualize-state_machines-with-graphviz/</guid>
		<description><![CDATA[The guys at Plugin-a-week have written a nice ActiveRecord state machine gem called state_machine. 
One of the great things about state machines is that they often can be visualized. To that end I&#8217;ve written a small utility that will load a class that uses state_machine and produce dot output which you can use with graphviz.
This [...]]]></description>
			<content:encoded><![CDATA[<p>The guys at <a href="http://www.pluginaweek.org/">Plugin-a-week</a> have written a nice ActiveRecord state machine gem called <a href="http://github.com/pluginaweek/state_machine/tree/master">state_machine</a>. </p>
<p>One of the great things about state machines is that they often can be visualized. To that end I&#8217;ve written a small utility that will load a class that uses <tt>state_machine</tt> and produce <tt>dot</tt> output which you can use with <a href="http://www.graphviz.org/">graphviz</a>.</p>
<p>This gem is currently used by the <a href="http://spreehq.org/">spree</a> shopping cart. Let&#8217;s look at an example of how this can be used to visualize the <tt>Order</tt> object in this project:</p>
<p>Save the gist below to <tt>visualize.rb</tt> and <tt>chmod +x visualize.rb</tt>. The arguments are 1) the path to <tt>RAILS_ROOT</tt> of the project using <tt>state_machine</tt> and 2) the class you want to visualize. Try <tt>visualize.rb --help</tt> to see all the options. </p>
<p><code><br />
 ruby visualize.rb . Order | dot -Tsvg -Grankdir=LR > graph.svg<br />
</code></p>
<p>Gives us the following image:</p>
<p><a href='http://www.xcombinator.com/wp-content/uploads/2008/10/picture-1.png' title='Spree State Machine'><img src='http://www.xcombinator.com/wp-content/uploads/2008/10/picture-1.thumbnail.png' alt='Spree State Machine' /></a></p>
<p>Download the <a href="http://gist.github.com/raw/16729/2ebb0d8e55803ca88aaf92e3278499623264360a">raw code</a> (Requires &#8216;trollop&#8217; gem).</p>
<p><script src="http://gist.github.com/16729.js"></script></p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2008%2F10%2F14%2Fvisualize-state_machines-with-graphviz%2F&amp;title=visualize%20%3Ctt%3Estate_machine%3C%2Ftt%3E%27s%20with%20graphviz&amp;notes=The%20guys%20at%20Plugin-a-week%20have%20written%20a%20nice%20ActiveRecord%20state%20machine%20gem%20called%20state_machine.%20%0D%0A%0D%0AOne%20of%20the%20great%20things%20about%20state%20machines%20is%20that%20they%20often%20can%20be%20visualized.%20To%20that%20end%20I%27ve%20written%20a%20small%20utility%20that%20will%20load%20a%20class%20" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2008%2F10%2F14%2Fvisualize-state_machines-with-graphviz%2F&amp;title=visualize%20%3Ctt%3Estate_machine%3C%2Ftt%3E%27s%20with%20graphviz" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2008%2F10%2F14%2Fvisualize-state_machines-with-graphviz%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=visualize%20%3Ctt%3Estate_machine%3C%2Ftt%3E%27s%20with%20graphviz%20-%20http%3A%2F%2Feigenjoy.com%2F2008%2F10%2F14%2Fvisualize-state_machines-with-graphviz%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2008%2F10%2F14%2Fvisualize-state_machines-with-graphviz%2F&amp;t=visualize%20%3Ctt%3Estate_machine%3C%2Ftt%3E%27s%20with%20graphviz" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2008%2F10%2F14%2Fvisualize-state_machines-with-graphviz%2F&amp;title=visualize%20%3Ctt%3Estate_machine%3C%2Ftt%3E%27s%20with%20graphviz&amp;annotation=The%20guys%20at%20Plugin-a-week%20have%20written%20a%20nice%20ActiveRecord%20state%20machine%20gem%20called%20state_machine.%20%0D%0A%0D%0AOne%20of%20the%20great%20things%20about%20state%20machines%20is%20that%20they%20often%20can%20be%20visualized.%20To%20that%20end%20I%27ve%20written%20a%20small%20utility%20that%20will%20load%20a%20class%20" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2008%2F10%2F14%2Fvisualize-state_machines-with-graphviz%2F&amp;t=visualize%20%3Ctt%3Estate_machine%3C%2Ftt%3E%27s%20with%20graphviz" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2008%2F10%2F14%2Fvisualize-state_machines-with-graphviz%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2008/10/14/visualize-state_machines-with-graphviz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveRecord from_xml (and from_json) part 2</title>
		<link>http://eigenjoy.com/2008/08/11/activerecord-from_xml-and-from_json-part-2/</link>
		<comments>http://eigenjoy.com/2008/08/11/activerecord-from_xml-and-from_json-part-2/#comments</comments>
		<pubDate>Tue, 12 Aug 2008 02:33:17 +0000</pubDate>
		<dc:creator>Matt Pulver</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[from_json]]></category>
		<category><![CDATA[from_xml]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[unmarshall]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2008/08/11/activerecord-from_xml-and-from_json-part-2/</guid>
		<description><![CDATA[This post is an upgrade to the previous post about the unmarshalling of XML and JSON strings into rails objects, with arbitrarily deep object associations. As you may know if you are reading this, there doesn&#8217;t seem to be a way in rails to reverse to_xml and to_json when associations are included.
Example usage:

xml = firm.to_xml [...]]]></description>
			<content:encoded><![CDATA[<p>This post is an upgrade to <a href="http://www.xcombinator.com/2008/07/06/activerecord-from_json-and-from_xml/">the previous post</a> about the unmarshalling of XML and JSON strings into rails objects, with arbitrarily deep object associations. As you may know if you are reading this, there doesn&#8217;t seem to be a way in rails to reverse to_xml and to_json when associations are included.</p>
<p>Example usage:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">xml = firm.<span style="color:#9900CC;">to_xml</span> <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:account</span>, <span style="color:#ff3333; font-weight:bold;">:clients</span> <span style="color:#006600; font-weight:bold;">&#93;</span>  
firm = Firm.<span style="color:#9900CC;">from_xml</span> xml</pre></div></div>

<p>firm.account and firm.clients will behave as intended (by default there does not seem to be a direct way in rails to unmarshall them.)</p>
<p>Here&#8217;s how to get from_xml and from_json defined for your ActiveRecord classes:</p>
<p>Create a file lib/extensions.rb with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecord
  <span style="color:#9966CC; font-weight:bold;">class</span> Base
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">from_hash</span><span style="color:#006600; font-weight:bold;">&#40;</span> hash <span style="color:#006600; font-weight:bold;">&#41;</span>
      h = hash.<span style="color:#9900CC;">dup</span>
      h.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>key,value<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#9966CC; font-weight:bold;">case</span> value.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">to_s</span>
        <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">'Array'</span>
          h<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span>! <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> reflect_on_association<span style="color:#006600; font-weight:bold;">&#40;</span>
             key.<span style="color:#9900CC;">to_sym</span> <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">klass</span>.<span style="color:#9900CC;">from_hash</span> e <span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>\AHash<span style="color:#006600; font-weight:bold;">&#40;</span>WithIndifferentAccess<span style="color:#006600; font-weight:bold;">&#41;</span>?\Z<span style="color:#006600; font-weight:bold;">/</span>
          h<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span> = reflect_on_association<span style="color:#006600; font-weight:bold;">&#40;</span>
             key.<span style="color:#9900CC;">to_sym</span> <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">klass</span>.<span style="color:#9900CC;">from_hash</span> value
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      new h
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">from_json</span><span style="color:#006600; font-weight:bold;">&#40;</span> json <span style="color:#006600; font-weight:bold;">&#41;</span>
      from_hash safe_json_decode<span style="color:#006600; font-weight:bold;">&#40;</span> json <span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># The xml has a surrounding class tag (e.g. ship-to),</span>
    <span style="color:#008000; font-style:italic;"># but the hash has no counterpart (e.g. 'ship_to' =&gt; {} )</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">from_xml</span><span style="color:#006600; font-weight:bold;">&#40;</span> xml <span style="color:#006600; font-weight:bold;">&#41;</span>
      from_hash <span style="color:#9966CC; font-weight:bold;">begin</span>
        <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">from_xml</span><span style="color:#006600; font-weight:bold;">&#40;</span>xml<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span>to_s.<span style="color:#9900CC;">demodulize</span>.<span style="color:#9900CC;">underscore</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">rescue</span> ; <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#008000; font-style:italic;"># class Base</span>
<span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#008000; font-style:italic;"># module ActiveRecord</span>
&nbsp;
<span style="color:#008000; font-style:italic;">### Global functions ###</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># JSON.decode, or return {} if anything goes wrong.</span>
<span style="color:#9966CC; font-weight:bold;">def</span> safe_json_decode<span style="color:#006600; font-weight:bold;">&#40;</span> json <span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">if</span> !json
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    <span style="color:#6666ff; font-weight:bold;">ActiveSupport::JSON</span>.<span style="color:#9900CC;">decode</span> json
  <span style="color:#9966CC; font-weight:bold;">rescue</span> ; <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>At the bottom of config/environments.rb, insert the line:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'lib/extensions'</span> <span style="color:#008000; font-style:italic;"># custom class extensions</span></pre></div></div>

<p>You can name the &#8220;extensions.rb&#8221; file anything else you want; just update the environments.rb file accordingly.</p>
<p>This is an improvement over the previous post, in that it can work even when your associations use the :class_name parameter, you&#8217;re not editing the vendor/rails files directly, and it can deal with hashes of the rails class HashWithIndifferentAccess.</p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2008%2F08%2F11%2Factiverecord-from_xml-and-from_json-part-2%2F&amp;title=ActiveRecord%20from_xml%20%28and%20from_json%29%20part%202&amp;notes=This%20post%20is%20an%20upgrade%20to%20the%20previous%20post%20about%20the%20unmarshalling%20of%20XML%20and%20JSON%20strings%20into%20rails%20objects%2C%20with%20arbitrarily%20deep%20object%20associations.%20As%20you%20may%20know%20if%20you%20are%20reading%20this%2C%20there%20doesn%27t%20seem%20to%20be%20a%20way%20in%20rails%20to%20reverse%20to" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2008%2F08%2F11%2Factiverecord-from_xml-and-from_json-part-2%2F&amp;title=ActiveRecord%20from_xml%20%28and%20from_json%29%20part%202" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2008%2F08%2F11%2Factiverecord-from_xml-and-from_json-part-2%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=ActiveRecord%20from_xml%20%28and%20from_json%29%20part%202%20-%20http%3A%2F%2Feigenjoy.com%2F2008%2F08%2F11%2Factiverecord-from_xml-and-from_json-part-2%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2008%2F08%2F11%2Factiverecord-from_xml-and-from_json-part-2%2F&amp;t=ActiveRecord%20from_xml%20%28and%20from_json%29%20part%202" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2008%2F08%2F11%2Factiverecord-from_xml-and-from_json-part-2%2F&amp;title=ActiveRecord%20from_xml%20%28and%20from_json%29%20part%202&amp;annotation=This%20post%20is%20an%20upgrade%20to%20the%20previous%20post%20about%20the%20unmarshalling%20of%20XML%20and%20JSON%20strings%20into%20rails%20objects%2C%20with%20arbitrarily%20deep%20object%20associations.%20As%20you%20may%20know%20if%20you%20are%20reading%20this%2C%20there%20doesn%27t%20seem%20to%20be%20a%20way%20in%20rails%20to%20reverse%20to" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2008%2F08%2F11%2Factiverecord-from_xml-and-from_json-part-2%2F&amp;t=ActiveRecord%20from_xml%20%28and%20from_json%29%20part%202" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2008%2F08%2F11%2Factiverecord-from_xml-and-from_json-part-2%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2008/08/11/activerecord-from_xml-and-from_json-part-2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>ActiveRecord from_json and from_xml</title>
		<link>http://eigenjoy.com/2008/07/06/activerecord-from_json-and-from_xml/</link>
		<comments>http://eigenjoy.com/2008/07/06/activerecord-from_json-and-from_xml/#comments</comments>
		<pubDate>Sun, 06 Jul 2008 20:27:25 +0000</pubDate>
		<dc:creator>Matt Pulver</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[from_json]]></category>
		<category><![CDATA[from_xml]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[to_json]]></category>
		<category><![CDATA[to_xml]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2008/07/06/activerecord-from_json-and-from_xml/</guid>
		<description><![CDATA[(Edited 2 Sept 2011: Please see instead the updated post here: ActiveRecord from_xml (and from_json) part 2.)
Unless I&#8217;m missing something, the default rails from_json and from_xml methods don&#8217;t work with data that have associated objects. Here are routines that work for some simple examples I&#8217;ve tested. I placed this code in vendor/rails/activerecord/lib/active_record/base.rb within the ActiveRecord::Base [...]]]></description>
			<content:encoded><![CDATA[<p>(Edited 2 Sept 2011: Please see instead the updated post here: <a href="http://www.xcombinator.com/2008/08/11/activerecord-from_xml-and-from_json-part-2/">ActiveRecord from_xml (and from_json) part 2</a>.)</p>
<p>Unless I&#8217;m missing something, the default rails from_json and from_xml methods don&#8217;t work with data that have associated objects. Here are routines that work for some simple examples I&#8217;ve tested. I placed this code in vendor/rails/activerecord/lib/active_record/base.rb within the ActiveRecord::Base class, but there might be a better place to put it.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">from_hash</span><span style="color:#006600; font-weight:bold;">&#40;</span> hash <span style="color:#006600; font-weight:bold;">&#41;</span>
  h = hash.<span style="color:#9900CC;">dup</span>
  h.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>key,value<span style="color:#006600; font-weight:bold;">|</span> 
    <span style="color:#9966CC; font-weight:bold;">case</span> value.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">to_s</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">'Array'</span>
      h<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span>! <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span>
         <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span>key.<span style="color:#9900CC;">camelize</span>.<span style="color:#9900CC;">singularize</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">from_hash</span> e <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">'Hash'</span>
      h<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">const_get</span><span style="color:#006600; font-weight:bold;">&#40;</span>key.<span style="color:#9900CC;">camelize</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">from_hash</span> value
    <span style="color:#9966CC; font-weight:bold;">end</span> 
  <span style="color:#9966CC; font-weight:bold;">end</span>     
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">new</span> h
<span style="color:#9966CC; font-weight:bold;">end</span>   
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">from_json</span><span style="color:#006600; font-weight:bold;">&#40;</span> json <span style="color:#006600; font-weight:bold;">&#41;</span>
  hash = <span style="color:#6666ff; font-weight:bold;">ActiveSupport::JSON</span>.<span style="color:#9900CC;">decode</span> json
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">from_hash</span> hash
<span style="color:#9966CC; font-weight:bold;">end</span>   
&nbsp;
<span style="color:#008000; font-style:italic;"># The xml has a surrounding class tag (e.g. ship-to),</span>
<span style="color:#008000; font-style:italic;"># but the hash has no counterpart (e.g. 'ship_to' =&gt; {} )</span>
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">from_xml</span><span style="color:#006600; font-weight:bold;">&#40;</span> xml <span style="color:#006600; font-weight:bold;">&#41;</span>
  hash = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">from_xml</span> xml
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">from_hash</span> hash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">underscore</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Also available on <a href="http://pastie.org/228617">pastie</a>.</p>
<p>Example usage, using the <a href="http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html">to_xml example</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">xml = firm.<span style="color:#9900CC;">to_xml</span> <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#ff3333; font-weight:bold;">:account</span>, <span style="color:#ff3333; font-weight:bold;">:clients</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
firm = Firm.<span style="color:#9900CC;">from_xml</span> xml</pre></div></div>

<p>Note: this only works if the classes of the nested objects are canonically named. If any of your associations use the :class_name option, you will probably need to update this code to discover the proper class names.</p>
<p>I just wrote this a couple hours ago, so please consider this to be an alpha version.</p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2008%2F07%2F06%2Factiverecord-from_json-and-from_xml%2F&amp;title=ActiveRecord%20from_json%20and%20from_xml&amp;notes=%28Edited%202%20Sept%202011%3A%20Please%20see%20instead%20the%20updated%20post%20here%3A%20ActiveRecord%20from_xml%20%28and%20from_json%29%20part%202.%29%0D%0A%0D%0AUnless%20I%27m%20missing%20something%2C%20the%20default%20rails%20from_json%20and%20from_xml%20methods%20don%27t%20work%20with%20data%20that%20have%20associated%20objects.%20Here%20ar" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2008%2F07%2F06%2Factiverecord-from_json-and-from_xml%2F&amp;title=ActiveRecord%20from_json%20and%20from_xml" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2008%2F07%2F06%2Factiverecord-from_json-and-from_xml%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=ActiveRecord%20from_json%20and%20from_xml%20-%20http%3A%2F%2Feigenjoy.com%2F2008%2F07%2F06%2Factiverecord-from_json-and-from_xml%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2008%2F07%2F06%2Factiverecord-from_json-and-from_xml%2F&amp;t=ActiveRecord%20from_json%20and%20from_xml" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2008%2F07%2F06%2Factiverecord-from_json-and-from_xml%2F&amp;title=ActiveRecord%20from_json%20and%20from_xml&amp;annotation=%28Edited%202%20Sept%202011%3A%20Please%20see%20instead%20the%20updated%20post%20here%3A%20ActiveRecord%20from_xml%20%28and%20from_json%29%20part%202.%29%0D%0A%0D%0AUnless%20I%27m%20missing%20something%2C%20the%20default%20rails%20from_json%20and%20from_xml%20methods%20don%27t%20work%20with%20data%20that%20have%20associated%20objects.%20Here%20ar" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2008%2F07%2F06%2Factiverecord-from_json-and-from_xml%2F&amp;t=ActiveRecord%20from_json%20and%20from_xml" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2008%2F07%2F06%2Factiverecord-from_json-and-from_xml%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2008/07/06/activerecord-from_json-and-from_xml/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>RailsConf08: Passenger or mod_rails RIP</title>
		<link>http://eigenjoy.com/2008/06/02/railsconf08-passenger-or-mod_rails-rip/</link>
		<comments>http://eigenjoy.com/2008/06/02/railsconf08-passenger-or-mod_rails-rip/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 22:43:25 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[railsconf08]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2008/06/02/railsconf08-passenger-or-mod_rails-rip/</guid>
		<description><![CDATA[There was no lack of hubris on the stage today as the guys from Phusion talked about their new Apache extension Passenger. If Passenger lives up to its claims it seems that it could quickly become the de-facto standard for deploying Rails (and more) applications.
The 19 22-year-old duo was obviously ecstatic about sharing about what [...]]]></description>
			<content:encoded><![CDATA[<p>There was no lack of hubris on the stage today as the guys from <a href="http://www.modrails.com/">Phusion</a> talked about their new Apache extension <b>Passenger</b>. If Passenger lives up to its claims it seems that it could quickly become the de-facto standard for deploying Rails (and more) applications.</p>
<p>The <strike>19</strike> 22-year-old duo was obviously ecstatic about sharing about what they created but I kept getting the feeling that they were surprised that the crowd didn&#8217;t give them a presidential-state-of-the-union-like standing-ovation every few minutes. (If passenger does what they claim maybe they deserve it but respect and appreciation often lose something when they are too eagerly expected.)</p>
<p>So what was it that they claimed? That passenger will not only make Rails deployment dead-simple (think PHP, upload and go) but also crank out better performance while using less memory. It&#8217;s a worthy goal and as <a href="http://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a> said in our keynote, &#8220;Humility is not a prerequisite to ideas with impact&#8221;. I&#8217;d like to write up a bit about Passenger and the session they presented at RailsConf. You can download the Keynote slides <a href="http://en.oreilly.com/rails2008/public/asset/attachment/2643">here</a> [zip] (I&#8217;m sure the PDF will appear somewhere soon. If you find it, please feel free to leave a URL in the comments).</p>
<h3>Memory Usage and Clustering</h3>
<p><b>Memory Usage</b></p>
<p>First lets talk about memory usage. When you&#8217;re using Mongrel each Mongrel process holds both a full copy of the Rails and application code in memory plus the private memory for the individual process. In this model of N Mongrel processes you have N copies of the application code. With Passenger, each process shares one copy of your Rails/application code. Each process still gets its own chunk of private memory but the shared code greatly reduces the overall memory usage.</p>
<p>The Phusion guys also patched Ruby (and they&#8217;ve horribly named it &#8220;Ruby Enterprise Edition&#8221;). This version has modified garbage collection and causes Ruby to use significantly less memory. They achieve this by doing <a href="http://en.wikipedia.org/wiki/Copy-on-write">copy-on-write</a> for memory management. This hasn&#8217;t been released so no word yet on how well it works or how stable it is.</p>
<p><b>Clustering</b></p>
<p>Another nice feature is that with clustering they use &#8220;fair load balancing&#8221;. The idea is that you keep track of how many jobs each process in the cluster has and you give the next job to the process with the least amount of work to do.</p>
<h3>Competitor Comparison</h3>
<p>They compared Passenger&#8217;s performance (as an Apache extension) to many competing products (including Nginx and Mongrel) and claimed that it used significantly less memory and was much faster. I won&#8217;t repeat all the statistics, you can check out the slides.</p>
<h3>mod_rails, RIP</h3>
<p>Although they had greatly simplified deployment for Rails they didn&#8217;t stop there. <b>Passenger now supports <a href="http://rack.rubyforge.org/">Rack</a></b>. I see this as probably the coolest thing about Passenger. Now any custom server that you write using Rack can be basically &#8220;dropped&#8221; into Apache and is effortlessly handled by Passenger. This also means that <b>rails alternatives like <a href="http://merbivore.com/">Merb</a> and <a href="http://code.whytheluckystiff.net/camping">Camping</a> work out of the box</b>. But there is more&#8230;</p>
<p>They also added in <i>support for Python&#8217;s Django</i>. It was almost comical: when they announced this the crowd nearly boo&#8217;ed them. I think it was a bit unfair, but I guess they should have expected it at a Rails conference. Either way, you have to give them props for taking the initiative and pushing the software to its boundaries.</p>
<p>Because Passenger supports frameworks other than Rails they decided to drop the name mod_rails and call it Phusion Passenger. They mentioned that their focus is still going to be developing and perfecting Rails. Passenger will simply not be exclusive to Rails.</p>
<h3>Case Studies</h3>
<p>Passenger is already being used in production at Dreamhost. It is also being used <a href="http://www.soocial.com">soocial</a> and <a href="http://www.ilike.com/">ilike</a>.</p>
<h3>The Q&amp;A</h3>
<p> By the Q&amp;A the crowd was full of skeptics; what they promised seemed too good to be true. However, one gentleman from the crowd sensed this, stood up, and said that he works with a developer/deployer Tom that has been working with production Rails deployment for 4 years. Tom apparently knows the in&#8217;s and out&#8217;s of Mongrel, Monit, Capistrano, etc. They found out about Passenger two or three weeks ago and he deployed all of their existing applications to Passenger in <b>a single day</b>. His comment was: </p>
<blockquote><p>Everything I learned [about Rails deployment] over the last few years is moot now, and that is a good thing.<i>Tom the deployer</i></p>
</blockquote>
<p>He said that &#8220;[Passenger] is incredibly awesome when it comes to rails deployment.&#8221; </p>
<p>A skeptic stood and rightly asked: &#8220;Why wasn&#8217;t this done five years ago? What was the technological hurdle?&#8221; The team answered that they believed the problem was largely social. Developers that had written Rails applications wanted to deploy them as quick as possible. They researched, learned about Capistano, Nginx, and Mongrel etc., and made it work. The Phusion team said that the people that were smart enough to tackle this problem were complacent and choose to deploy applications in this (painful) way.</p>
<blockquote><p>There is nothing technically preventing [ease of rails deployment]. We&#8217;ve shown it&#8217;s possible. Why it hasn&#8217;t been done is a social or political problem. There is no technical things stopping you. <i>-Hongli Lai (Phusion)</i></p>
</blockquote>
<h3>Summary</h3>
<p>Time will tell us if Apache/Passenger will live up to the hype and become the new standard. I, for one, am hoping that it really does take hold. If Rails deployment becomes as common and easy as PHP deployment we can spend time solving more interesting problems and that will be a good thing.</p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2008%2F06%2F02%2Frailsconf08-passenger-or-mod_rails-rip%2F&amp;title=RailsConf08%3A%20Passenger%20or%20mod_rails%20RIP&amp;notes=There%20was%20no%20lack%20of%20hubris%20on%20the%20stage%20today%20as%20the%20guys%20from%20Phusion%20talked%20about%20their%20new%20Apache%20extension%20Passenger.%20If%20Passenger%20lives%20up%20to%20its%20claims%20it%20seems%20that%20it%20could%20quickly%20become%20the%20de-facto%20standard%20for%20deploying%20Rails%20%28and%20more%29%20" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2008%2F06%2F02%2Frailsconf08-passenger-or-mod_rails-rip%2F&amp;title=RailsConf08%3A%20Passenger%20or%20mod_rails%20RIP" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2008%2F06%2F02%2Frailsconf08-passenger-or-mod_rails-rip%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=RailsConf08%3A%20Passenger%20or%20mod_rails%20RIP%20-%20http%3A%2F%2Feigenjoy.com%2F2008%2F06%2F02%2Frailsconf08-passenger-or-mod_rails-rip%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2008%2F06%2F02%2Frailsconf08-passenger-or-mod_rails-rip%2F&amp;t=RailsConf08%3A%20Passenger%20or%20mod_rails%20RIP" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2008%2F06%2F02%2Frailsconf08-passenger-or-mod_rails-rip%2F&amp;title=RailsConf08%3A%20Passenger%20or%20mod_rails%20RIP&amp;annotation=There%20was%20no%20lack%20of%20hubris%20on%20the%20stage%20today%20as%20the%20guys%20from%20Phusion%20talked%20about%20their%20new%20Apache%20extension%20Passenger.%20If%20Passenger%20lives%20up%20to%20its%20claims%20it%20seems%20that%20it%20could%20quickly%20become%20the%20de-facto%20standard%20for%20deploying%20Rails%20%28and%20more%29%20" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2008%2F06%2F02%2Frailsconf08-passenger-or-mod_rails-rip%2F&amp;t=RailsConf08%3A%20Passenger%20or%20mod_rails%20RIP" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2008%2F06%2F02%2Frailsconf08-passenger-or-mod_rails-rip%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2008/06/02/railsconf08-passenger-or-mod_rails-rip/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>RailsConf08: Testing and Contributing to Open-Source</title>
		<link>http://eigenjoy.com/2008/05/30/testing-and-contributing-to-open-source/</link>
		<comments>http://eigenjoy.com/2008/05/30/testing-and-contributing-to-open-source/#comments</comments>
		<pubDate>Fri, 30 May 2008 14:47:01 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[railsconf]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2008/05/30/testing-and-contributing-to-open-source/</guid>
		<description><![CDATA[Yesterday afternoon I attended a lecture and workshop on how to contribute to open-source projects and associated testing tools. I want to briefly share with you some of the tools and philosophies.
rcov
rcov.  rcov is a code coverage tool for Ruby. The idea is that you run it on your tests. Lines that get executed [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday afternoon I attended a lecture and workshop on how to contribute to open-source projects and associated testing tools. I want to briefly share with you some of the tools and philosophies.</p>
<h3>rcov</h3>
<p><a href="http://eigenclass.org/hiki.rb?rcov">rcov</a>. <a href="http://www.flickr.com/photos/nate_murray/2534584821/" title="rcov example by Nate Murray, on Flickr"><img src="http://farm4.static.flickr.com/3221/2534584821_d367d8fa9f.jpg" width="500" height="305" alt="rcov example" /></a> rcov is a code coverage tool for Ruby. The idea is that you run it on your tests. Lines that get <b>executed</b> are green and &#8220;ok&#8221; and lines that are never executed are marked red for being dangerous because they are never tested. This is an easy way for you to clearly see which lines of code are never being touched by any of your test cases.</p>
<p>I think that rcov will significantly change the way I approach testing with my own team and consultants. I&#8217;ll have to talk to my team, but it seems reasonable to me that every piece of code that is going to be used in production should have at least 50% code coverage. In growing, changing production systems this seems like a very valuable way to keep the code malleable (because you can refactor with less fear of breaking) which then leads to increased confidence and reduced cost-of-ownership.</p>
<h3>flog</h3>
<p><a href="http://ruby.sadi.st/Flog.html">flog</a> &#8211; a ruby code complexity analyzer. Now the first question you may be asking is &#8220;what in the world is a complexity analyzer?&#8221; The idea is that flog reads your code and assigns it a score based on criteria which is determined by its author, Ryan Davis. Basically it rates on the type of code Ryan Davis likes to see. This isn&#8217;t as terrible as it may sound because Ryan has some very tasteful ideas on what readable code looks like.</p>
<p>You may or may not agree with the way Ryan scores things, but as our speaker pointed out, flog and rcov <i>often agree with each other</i> about where a program&#8217;s problem areas are. More often than not the high flog score areas are not tested because they are too bulky to be tested. When one writes tests you often have to design the code in smaller, more testable pieces. Readability and testability often go hand in hand.</p>
<p>When running flog it can be helpful to just grep the output for <tt>#</tt>. This will give a high level view of the methods and their scores. Good candidates for refactoring are methods with scores between 80-150. Higher than 150 are typically serious problem areas (in terms of readability). Make sure you have them very well tested before you try to refactor them too much.</p>
<h3>heckle</h3>
<p>From the <a href="http://glu.ttono.us/articles/2006/12/19/tormenting-your-tests-with-heckle">heckle</a> website: &#8220;Heckle is a mutation tester. It modifies your code and runs your tests to make sure they fail. The idea is that if code can be changed and your tests don&#8217;t notice, either that code isn&#8217;t being covered or it doesn&#8217;t anything.&#8221; The idea is that hackle tests your tests and makes sure you are actually writing tests that are meaningful.</p>
<h3>tarantula</h3>
<p><a href="http://blog.thinkrelevance.com/2008/2/26/tarantula-vs-your-rails-app">tarantula</a> is a &#8220;fuzzy spider&#8221;. It crawls your web application and submits tons of garbage data and tries raise exceptions in your rails application. The premise of this tool is that no matter what data is posted, your application should not be returning 500 errors. You should handle bad data gracefully and not let unnecessary exceptions bubble to the surface. (I&#8217;m not entirely clear on how this gels with RESTful development where one <a href="http://www.therailsway.com/2007/9/3/using-activeresource-to-consume-web-services">often uses the return status codes</a> to return meaningful information)</p>
<h3>summary</h3>
<p>It was pointed out that none of these tools replace thought; they are here to enhance it. flog, for instance, is a tool that helps enhance your sense of code-smell. You may run flog, look at a &#8220;complex&#8221; method and decide that it is perfectly acceptable for your system. Obviously, you as the programmer know better than an automated tool. However, it is often the case that flog will point out passages of code that are challenging and difficult for anyone but the original author to read. The idea is to encourage code that is simple to test, read, and understand. This makes the code more robust and open to change.</p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F30%2Ftesting-and-contributing-to-open-source%2F&amp;title=RailsConf08%3A%20Testing%20and%20Contributing%20to%20Open-Source&amp;notes=Yesterday%20afternoon%20I%20attended%20a%20lecture%20and%20workshop%20on%20how%20to%20contribute%20to%20open-source%20projects%20and%20associated%20testing%20tools.%20I%20want%20to%20briefly%20share%20with%20you%20some%20of%20the%20tools%20and%20philosophies.%20%0D%0A%0D%0Arcov%0D%0A%0D%0Arcov.%20%20rcov%20is%20a%20code%20coverage%20tool%20for%20" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F30%2Ftesting-and-contributing-to-open-source%2F&amp;title=RailsConf08%3A%20Testing%20and%20Contributing%20to%20Open-Source" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F30%2Ftesting-and-contributing-to-open-source%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=RailsConf08%3A%20Testing%20and%20Contributing%20to%20Open-Source%20-%20http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F30%2Ftesting-and-contributing-to-open-source%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F30%2Ftesting-and-contributing-to-open-source%2F&amp;t=RailsConf08%3A%20Testing%20and%20Contributing%20to%20Open-Source" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F30%2Ftesting-and-contributing-to-open-source%2F&amp;title=RailsConf08%3A%20Testing%20and%20Contributing%20to%20Open-Source&amp;annotation=Yesterday%20afternoon%20I%20attended%20a%20lecture%20and%20workshop%20on%20how%20to%20contribute%20to%20open-source%20projects%20and%20associated%20testing%20tools.%20I%20want%20to%20briefly%20share%20with%20you%20some%20of%20the%20tools%20and%20philosophies.%20%0D%0A%0D%0Arcov%0D%0A%0D%0Arcov.%20%20rcov%20is%20a%20code%20coverage%20tool%20for%20" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F30%2Ftesting-and-contributing-to-open-source%2F&amp;t=RailsConf08%3A%20Testing%20and%20Contributing%20to%20Open-Source" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F30%2Ftesting-and-contributing-to-open-source%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2008/05/30/testing-and-contributing-to-open-source/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RailsConf08: Meta-programming Ruby for Fun and Profit</title>
		<link>http://eigenjoy.com/2008/05/29/railsconf08-meta-programming-ruby-for-fun-and-profit/</link>
		<comments>http://eigenjoy.com/2008/05/29/railsconf08-meta-programming-ruby-for-fun-and-profit/#comments</comments>
		<pubDate>Fri, 30 May 2008 01:07:40 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[railsconf]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2008/05/29/railsconf08-meta-programming-ruby-for-fun-and-profit/</guid>
		<description><![CDATA[ I&#8217;m currently at RailsConf and it is fantastic. I&#8217;ve met a good number of interesting people and attended some interesting sessions.
Ruby Internals
 This morning Neal Ford and Patrick Farley gave a great session on Meta-programming Ruby for Fun and Profit. The slides should eventually show up
here.
Particularly interesting was Patrick&#8217;s portion about the internals of [...]]]></description>
			<content:encoded><![CDATA[<p> I&#8217;m currently at RailsConf and it is fantastic. I&#8217;ve met a good number of interesting people and attended some interesting sessions.</p>
<h3>Ruby Internals</h3>
<p> This morning <a href="http://nealford.com/">Neal Ford</a> and <a href="http://www.klankboomklang.com/">Patrick Farley</a> gave a great session on <b>Meta-programming Ruby for Fun and Profit</b>. The slides should eventually show up<br />
<a href="http://nealford.com/mypastconferences.htm">here</a>.</p>
<p>Particularly interesting was Patrick&#8217;s portion about the internals of Ruby&#8217;s method dispatch and how it relates to inheritance, mixins, and the object&#8217;s eigenclass. The basic idea is that when you call a class-method in Ruby the eigenclass (or metaclass) has a parallel inheritance structure to the actual class. I may write more about this with diagrams after Patrick posts the slides, but the the understanding of the system leads to this koan-like ruby truth:</p>
<blockquote><p>
The super-class of the meta-class is the meta-class of the super-class
</p>
</blockquote>
<p>Don&#8217;t worry if it isn&#8217;t clear right away. <a href="http://geminstallthat.wordpress.com/">Reid</a> and I<br />
attended the session and then discussed it over launch and it still took a while to sink in. In the meantime, checkout _why&#8217;s article <a href="http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html">Seeing Metaclasses Clearly</a>.</p>
<h3>Use <tt>class_eval</tt> instead of reopening a class</h3>
<p>Patrick gave another very handy tip when dealing with meta-programming. He mentioned that using <tt>class_eval</tt> is much safer than re-opening a class and <tt>def</tt>ining a method. This is because you don&#8217;t always know when files are loaded and when you open a class you may be defining it without realizing it. When you use <tt>class_eval</tt> you are using an existing constant. For example:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#008000; font-style:italic;"># warning! this could be defining ExistingKlass and you don't even know it!</span>
<span style="color:#008000; font-style:italic;"># then the original ExisitingKlass will just overwrite your method</span>
<span style="color:#9966CC; font-weight:bold;">class</span> ExistingKlass
  <span style="color:#9966CC; font-weight:bold;">def</span> change_a_method
    <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># do this instead as it will not try to define ExistingKlass. class_eval</span>
<span style="color:#008000; font-style:italic;"># guarantees the class already exists</span>
ExistingKlass.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
  <span style="color:#9966CC; font-weight:bold;">def</span> change_a_method
    <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p></code></p>
<h3>A Recorder</h3>
<p>Neal shared with us a very interesting class that records the messages sent to it`and can play them back later on. Here&#8217;s the code for it:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># recorder.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Recorder
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@messages</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>method, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@messages</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>method, args, block<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> play_back_to<span style="color:#006600; font-weight:bold;">&#40;</span>obj<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@messages</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>method, args, block<span style="color:#006600; font-weight:bold;">|</span>
      obj.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>method, <span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>  
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># recorder_test.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> TestRecorder <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> test_recorder
    r = Recorder.<span style="color:#9900CC;">new</span>
    r.<span style="color:#CC0066; font-weight:bold;">sub!</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>Java<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;Ruby&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    r.<span style="color:#9900CC;">upcase</span>!
    r<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">11</span>, <span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Universe&quot;</span>
    r <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;!&quot;</span>
&nbsp;
    s = <span style="color:#996600;">&quot;Hello Java World&quot;</span>
    r.<span style="color:#9900CC;">play_back_to</span><span style="color:#006600; font-weight:bold;">&#40;</span>s<span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_equal <span style="color:#996600;">&quot;HELLO RUBY Universe!&quot;</span>, s
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p></code></p>
<p>I haven&#8217;t fully processed the power of this idea yet. However, I feel like it could have some application in genetic programming.</p>
<h3>Tabula Rasa, the Recorder, and DSLs</h3>
<p>Neal pointed out that one of the issues with <tt>Recorder</tt> is the following:</p>
<p><code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># recorder_test.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> TestRecorder <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> test_recorder
    r = Recorder.<span style="color:#9900CC;">new</span>
    r.<span style="color:#9900CC;">upcase</span>!
    r.<span style="color:#9900CC;">freeze</span>
&nbsp;
    s = <span style="color:#996600;">&quot;hi nate&quot;</span>
    r.<span style="color:#9900CC;">play_back_to</span><span style="color:#006600; font-weight:bold;">&#40;</span>s<span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_equal <span style="color:#996600;">&quot;HI NATE&quot;</span>, s
&nbsp;
    s.<span style="color:#9900CC;">downcase</span>! <span style="color:#008000; font-style:italic;"># &lt;&lt;&lt; shouldn't work because it is frozen!</span>
    assert_equal <span style="color:#996600;">&quot;hi name&quot;</span>, s
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p></code></p>
<p>The issue is that the <tt>#freeze</tt> method went to the <tt>Recorder</tt> and not to the string. This is a problem you are likely to run into with a class like this because a standard ruby <tt>Object</tt> contains about 40 other methods; methods who&#8217;s names may conflict with what you want to delegate or capture with <tt>#method_missing</tt> (like the recorder). Thankfully <a href="http://onestepback.org/">Jim Weirich</a> has created a <a href="http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc">BlankSlate</a> class that you can use that will undefine all of the existing methods. If you have <tt>Recorder</tt> inherit from <tt>BlankSlate</tt> it will then work as expected. Neal mentioned that this class is being integrated into ruby 1.9 as <tt>SimpleObject</tt>. </p>
<p>When the slides become available checkout the section on the <tt>Quantifier</tt> module. The code is a bit lengthy to reproduce here but worth a look.</p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F29%2Frailsconf08-meta-programming-ruby-for-fun-and-profit%2F&amp;title=RailsConf08%3A%20Meta-programming%20Ruby%20for%20Fun%20and%20Profit&amp;notes=%20I%27m%20currently%20at%20RailsConf%20and%20it%20is%20fantastic.%20I%27ve%20met%20a%20good%20number%20of%20interesting%20people%20and%20attended%20some%20interesting%20sessions.%0D%0A%0D%0ARuby%20Internals%0D%0A%20This%20morning%20Neal%20Ford%20and%20Patrick%20Farley%20gave%20a%20great%20session%20on%20Meta-programming%20Ruby%20for%20Fun%20" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F29%2Frailsconf08-meta-programming-ruby-for-fun-and-profit%2F&amp;title=RailsConf08%3A%20Meta-programming%20Ruby%20for%20Fun%20and%20Profit" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F29%2Frailsconf08-meta-programming-ruby-for-fun-and-profit%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=RailsConf08%3A%20Meta-programming%20Ruby%20for%20Fun%20and%20Profit%20-%20http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F29%2Frailsconf08-meta-programming-ruby-for-fun-and-profit%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F29%2Frailsconf08-meta-programming-ruby-for-fun-and-profit%2F&amp;t=RailsConf08%3A%20Meta-programming%20Ruby%20for%20Fun%20and%20Profit" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F29%2Frailsconf08-meta-programming-ruby-for-fun-and-profit%2F&amp;title=RailsConf08%3A%20Meta-programming%20Ruby%20for%20Fun%20and%20Profit&amp;annotation=%20I%27m%20currently%20at%20RailsConf%20and%20it%20is%20fantastic.%20I%27ve%20met%20a%20good%20number%20of%20interesting%20people%20and%20attended%20some%20interesting%20sessions.%0D%0A%0D%0ARuby%20Internals%0D%0A%20This%20morning%20Neal%20Ford%20and%20Patrick%20Farley%20gave%20a%20great%20session%20on%20Meta-programming%20Ruby%20for%20Fun%20" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F29%2Frailsconf08-meta-programming-ruby-for-fun-and-profit%2F&amp;t=RailsConf08%3A%20Meta-programming%20Ruby%20for%20Fun%20and%20Profit" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2008%2F05%2F29%2Frailsconf08-meta-programming-ruby-for-fun-and-profit%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2008/05/29/railsconf08-meta-programming-ruby-for-fun-and-profit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Chaining :include&#8217;s in Rails to reduce the number of SQL queries</title>
		<link>http://eigenjoy.com/2008/04/08/chaining-includes-in-rails-to-reduce-the-number-of-sql-queries/</link>
		<comments>http://eigenjoy.com/2008/04/08/chaining-includes-in-rails-to-reduce-the-number-of-sql-queries/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 17:51:33 +0000</pubDate>
		<dc:creator>Matt Pulver</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[include]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[joins]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2008/04/08/chaining-includes-in-rails-to-reduce-the-number-of-sql-queries/</guid>
		<description><![CDATA[Say you have the following data model

and you want to execute a single query that returns all the data at once within the ActiveRecord tables, with the proper rails associations between them. Wouldn&#8217;t it be nice if you could do something like

A.find&#40;:all, :include =&#62; :b =&#62; :c =&#62; :d =&#62; :e&#41; #v1

? Though this is [...]]]></description>
			<content:encoded><![CDATA[<p>Say you have the following data model</p>
<p><img src='http://www.xcombinator.com/wp-content/uploads/2008/04/associations.gif' alt='A-B-C-D-E' /></p>
<p>and you want to execute a single query that returns all the data at once within the ActiveRecord tables, with the proper rails associations between them. Wouldn&#8217;t it be nice if you could do something like</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">A.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:c</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:d</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:e</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#v1</span></pre></div></div>

<p>? Though this is not even valid ruby code, it actually comes very close to what you can do in Ruby on Rails. To get this right, let&#8217;s take a closer look at the rails associations within the class definitions:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> A <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:b</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> B <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:as</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:cs</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> C <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:b</span>
  has_one <span style="color:#ff3333; font-weight:bold;">:d</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> D <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:c</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:es</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">class</span> E <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:ds</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Let&#8217;s try the rails code again, putting an &#8217;s&#8217; after the :c and :e as required by rails in order to denote they are &#8220;many&#8221;-type associations:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">A.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:cs</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:d</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:es</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#v2</span></pre></div></div>

<p>That&#8217;s closer, but still not valid ruby code. To fix that, think of the => operator as being right-associative, and instead of putting in parentheses (), put in curly braces {} in order to create nested hashes:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">A.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:b <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:cs <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:d <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:es</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#v3</span></pre></div></div>

<p>That&#8217;s it! Looking in the logs, we see that this only produced a single query, with all the desired SQL joins:</p>
<p><code>A Load Including Associations (0.001088)   SELECT `as`.`id` AS t0_r0, `as`.`b_id` AS t0_r1, `bs`.`id` AS t1_r0, `cs`.`id` AS t2_r0, `cs`.`b_id` AS t2_r1, `cs`.`d_id` AS t2_r2, `ds`.`id` AS t3_r0, `ds`.`c_id` AS t3_r1, `es`.`id` AS t4_r0 FROM `as` LEFT OUTER JOIN `bs` ON `bs`.id = `as`.b_id LEFT OUTER JOIN `cs` ON cs.b_id = bs.id LEFT OUTER JOIN `ds` ON ds.c_id = cs.id LEFT OUTER JOIN `ds_es` ON `ds_es`.d_id = `ds`.id LEFT OUTER JOIN `es` ON `es`.id = `ds_es`.e_id</code></p>
<p>With this tool in mind, you can use this in any ActiveRecord function that accepts the :include option to reduce the number of times the rails app hits the database, and ultimately speed up your rails application.</p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;title=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries&amp;notes=Say%20you%20have%20the%20following%20data%20model%0D%0A%0D%0A%0D%0A%0D%0Aand%20you%20want%20to%20execute%20a%20single%20query%20that%20returns%20all%20the%20data%20at%20once%20within%20the%20ActiveRecord%20tables%2C%20with%20the%20proper%20rails%20associations%20between%20them.%20Wouldn%27t%20it%20be%20nice%20if%20you%20could%20do%20something%20like%0D" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;title=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries%20-%20http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;t=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;title=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries&amp;annotation=Say%20you%20have%20the%20following%20data%20model%0D%0A%0D%0A%0D%0A%0D%0Aand%20you%20want%20to%20execute%20a%20single%20query%20that%20returns%20all%20the%20data%20at%20once%20within%20the%20ActiveRecord%20tables%2C%20with%20the%20proper%20rails%20associations%20between%20them.%20Wouldn%27t%20it%20be%20nice%20if%20you%20could%20do%20something%20like%0D" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;t=Chaining%20%3Ainclude%27s%20in%20Rails%20to%20reduce%20the%20number%20of%20SQL%20queries" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F08%2Fchaining-includes-in-rails-to-reduce-the-number-of-sql-queries%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2008/04/08/chaining-includes-in-rails-to-reduce-the-number-of-sql-queries/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple Rails Log Query Analyzer</title>
		<link>http://eigenjoy.com/2008/04/01/simple-rails-log-query-analyzer/</link>
		<comments>http://eigenjoy.com/2008/04/01/simple-rails-log-query-analyzer/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 19:57:51 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2008/04/01/simple-rails-log-query-analyzer/</guid>
		<description><![CDATA[Intro
This is a simple rails log analyzer. The initial goal is to
identify SQL queries that could be optimized or find queries that are being
called more often than they should be.
In its current state it is little more than simple regexes. My goal, however is
to build more useful tools on top of it.  I&#8217;ve added [...]]]></description>
			<content:encoded><![CDATA[<h2>Intro</h2>
<p>This is a simple rails log analyzer. The initial goal is to<br />
identify SQL queries that could be optimized or find queries that are being<br />
called more often than they should be.</p>
<p>In its current state it is little more than simple regexes. My goal, however is<br />
to build more useful tools on top of it.  I&#8217;ve added it to a git repository to<br />
allow anyone to make edits and improvements.</p>
<p>It&#8217;s not really ready for prime-time (for instance, it doesn&#8217;t work well<br />
with log colorization), but I have found it useful so I thought I would share<br />
it. If nothing else it could save someone else 10 minutes to write<br />
a similar script.</p>
<h2>What it does</h2>
<p>Right now all this does is read a log file and extract SQL &#8220;SELECT&#8221; statements<br />
and organize them by the amount of time they take. It will also show a summary<br />
of the number of queries called to load each model.</p>
<h2>How to get it</h2>
<p><code><br />
  git clone \<br />
  git://gitorious.org/simple_rails_log_analyzer/mainline.git \<br />
  simple_rails_log_analyzer<br />
</code></p>
<h2>How to use it</h2>
<p>The typical work flow looks like this:<br />
<code><br />
   rake log:clear<br />
   # hit the URL of the page we want to profile, run the rake task, etc<br />
   ruby bin/query_log_analyzer.rb log/development.log<br />
</code></p>
<p>Or visit <a href="http://gitorious.org/projects/simple_rails_log_analyzer">http://gitorious.org/projects/simple_rails_log_analyzer</a></p>
<h2>TODO</h2>
<ul>
<li>Deal with the colorization of the logs</li>
<li>Update the code to not be so SELECT centric. Support other types of queries</li>
<li>Be smarter about each request&#8217;s individual queries</li>
</ul>
<h2>Contributions</h2>
<p>I&#8217;ve placed this in a git repository, so feel free to submit any patches and<br />
I&#8217;ll integrate them into this master.</p>
<h2>Similar Projects</h2>
<ul>
<li><a href="http://rails-analyzer.rubyforge.org/">http://rails-analyzer.rubyforge.org/</a></li>
</ul>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F01%2Fsimple-rails-log-query-analyzer%2F&amp;title=Simple%20Rails%20Log%20Query%20Analyzer&amp;notes=Intro%0D%0AThis%20is%20a%20simple%20rails%20log%20analyzer.%20The%20initial%20goal%20is%20to%0D%0Aidentify%20SQL%20queries%20that%20could%20be%20optimized%20or%20find%20queries%20that%20are%20being%0D%0Acalled%20more%20often%20than%20they%20should%20be.%0D%0A%0D%0AIn%20its%20current%20state%20it%20is%20little%20more%20than%20simple%20regexes.%20My%20" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F01%2Fsimple-rails-log-query-analyzer%2F&amp;title=Simple%20Rails%20Log%20Query%20Analyzer" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F01%2Fsimple-rails-log-query-analyzer%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=Simple%20Rails%20Log%20Query%20Analyzer%20-%20http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F01%2Fsimple-rails-log-query-analyzer%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F01%2Fsimple-rails-log-query-analyzer%2F&amp;t=Simple%20Rails%20Log%20Query%20Analyzer" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F01%2Fsimple-rails-log-query-analyzer%2F&amp;title=Simple%20Rails%20Log%20Query%20Analyzer&amp;annotation=Intro%0D%0AThis%20is%20a%20simple%20rails%20log%20analyzer.%20The%20initial%20goal%20is%20to%0D%0Aidentify%20SQL%20queries%20that%20could%20be%20optimized%20or%20find%20queries%20that%20are%20being%0D%0Acalled%20more%20often%20than%20they%20should%20be.%0D%0A%0D%0AIn%20its%20current%20state%20it%20is%20little%20more%20than%20simple%20regexes.%20My%20" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F01%2Fsimple-rails-log-query-analyzer%2F&amp;t=Simple%20Rails%20Log%20Query%20Analyzer" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2008%2F04%2F01%2Fsimple-rails-log-query-analyzer%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2008/04/01/simple-rails-log-query-analyzer/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

