<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - awesome_nested_set: making nested sets cool Changes</title>
  <id>tag:opensoul.org,2009:/2008/11/17/making-nested-sets-cool/changes</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://opensoul.org/2008/11/17/making-nested-sets-cool/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2008/11/17/making-nested-sets-cool" rel="alternate" type="text/html"/>
  <updated>2008-11-17T06:32:39Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2008-11-17:505:1</id>
    <updated>2008-11-17T06:32:39Z</updated>
    <link href="http://opensoul.org/2009/3/5/making-nested-sets-cool" rel="alternate" type="text/html"/>
    <title>Making nested sets cool</title>
<content type="html">&lt;p&gt;Yes, I&#8217;m making the assertion that preordered &lt;a href=&quot;http://en.wikipedia.org/wiki/Tree_traversal&quot;&gt;tree traversal&lt;/a&gt; is now cool. And I don&#8217;t mean just &#8220;pocket protector&#8221; cool, because it&#8217;s always been that, but now it&#8217;s &#8220;show your friends&#8221; and &#8220;make lots of money&#8221; cool.&lt;/p&gt;


	&lt;p&gt;For those that have no idea what I&#8217;m talking about, and don&#8217;t really care, but still want to be cool, skip to the next section. For all three of you that want to understand, check out this &lt;a href=&quot;http://dev.mysql.com/tech-resources/articles/hierarchical-data.html&quot;&gt;MySQL DevZone article on managing hierarchical data&lt;/a&gt;.&lt;/p&gt;


	&lt;h3&gt;What are you talking about?&lt;/h3&gt;


	&lt;p&gt;I&#8217;m talking about putting hierarchical data into a relational database. There are lots of reasons for trying to do this: organizational structures, genealogies, &lt;a href=&quot;http://en.wikipedia.org/wiki/Taxonomy&quot;&gt;taxonomies&lt;/a&gt;, nested pages of a website, etc. It&#8217;s kinda like putting a square peg into a round hole, except that the square peg is made out of &lt;a href=&quot;http://weblog.jamisbuck.org/2008/11/9/legos-play-doh-and-programming&quot;&gt;Play-Doh&lt;/a&gt;, so we can force it through the hole anyway, and we just have a little extra mess to deal with.&lt;/p&gt;


	&lt;p&gt;There were several Active Record plugins out there that tried to clean up the mess, but they were either buggy or incomplete.&lt;/p&gt;


	&lt;p&gt;We created &lt;a href=&quot;http://github.com/collectiveidea/awesome_nested_set&quot;&gt;awesome_nested_set&lt;/a&gt; to try to remedy that.&lt;/p&gt;


	&lt;h3&gt;What makes this so awesome?&lt;/h3&gt;


	&lt;p&gt;There&#8217;s a lot of things that makes this awesome—it&#8217;s well tested so there are no known bugs, it&#8217;s fairly feature complete–but my personal favorite is that awesome_nested_set makes use of Rails 2.1&#8217;s named_scope features&lt;sup&gt;&lt;a href=&quot;#named_scope_backport&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;, so most of the nested set methods return a scope that works as a finder. You can call find methods on it or access other named scopes.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Department &amp;lt; ActiveRecord::Base
  acts_as_nested_set
  named_scope :active, :conditions =&amp;gt; {:active =&amp;gt; true}
end

department = Department.first
active = department.descendants.active
rogue = department.descendants.all(:conditions =&amp;gt; 'manager_id IS NULL')&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;There&#8217;s lots more info in the &lt;a href=&quot;http://github.com/collectiveidea/awesome_nested_set&quot;&gt;&lt;span class=&quot;caps&quot;&gt;README&lt;/span&gt; on GitHub&lt;/a&gt;, so check it out.  Let us know if you have any suggestions or feedback.&lt;/p&gt;


&lt;ol class=&quot;footnotes&quot;&gt;
  &lt;li&gt;It also backports named_scope for those still on Rails 2.0&lt;/li&gt;
&lt;/ol&gt;</content>  </entry>
</feed>
