Is this your first visit? You may want to subscribe to the feed.

awesome_nested_set: making nested sets cool

Yes, I’m making the assertion that preordered tree traversal is now cool. And I don’t mean just “pocket protector” cool, because it’s always been that, but now it’s “show your friends” cool.

For those that have no idea what I’m talking about, and don’t really care, but still want to be cool, skip to the next section. For all three of you that want to understand all the gory details, check out this MySQL DevZone article on managing hierarchical data.

What are you talking about?

I’m talking about putting hierarchical data into a relational database, and a plugin to make that easier. There are lots of reasons for trying to do this: organizational structures, genealogies, taxonomies, nested pages of a website, etc. It’s kinda like putting a square peg into a round hole, except that the square peg is made out of Play-Doh, so we can force it through the hole anyway, and we just have a little extra mess to deal with.

There were several Active Record plugins out there that tried to clean up the mess, but they were either buggy or incomplete.

We created awesome_nested_set to try to remedy that.

What makes this so awesome?

There’s a lot of things that makes this awesome, but my personal favorite is that awesome_nested_set makes use of Rails 2.1’s named_scope features1, 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.

class Department < ActiveRecord::Base
  acts_as_nested_set
  named_scope :in_need_of_review, :lambda => {{
    :conditions => {:reviewed_at > 1.year.ago
  }}
end

chancellor = Department.create(:name => 'Chancellor')
aa = Department.create(:name => 'Academic Affairs').move_to_child_of(chancellor)
Department.create(:name => 'Admissions').move_to_child_of aa
Department.create(:name => 'Student Services',
  :reviewed_at => 3.months.ago).move_to_child_of aa

chancellor.descendants.in_need_of_review
rogue = chancellor.descendants.all(:conditions => 'manager_id IS NULL')

There’s lots more info in the README on GitHub, so check it out. Let us know if you have any suggestions or feedback.

  1. It also backports named_scope for those still on Rails 2.0
Code: awesome_nested_set, collectiveidea, plugin, rails Nov 17, 2008 ● updated Nov 17, 2008 3 comments

3 comments

  1. I have been using this plugin for several months and appreciate the improvements it has achieved over the original nested set Rails plugin. The original had some problems that were a hassle to figure out.

    awesome_nested_set works great, thanks!

    John John November 25, 2008 at 06:38 AM
  2. i really like the awesome_nested_set and i would like to use it together with activescaffold – but i can’t really figure out how to do it. could you post an example please?

    thanks!

    gerold gerold December 01, 2008 at 06:02 AM
  3. Really appreciate the plugin. I seem to have a bit of an issue and it very well may be related to my limited experience with rails.

    Calling new() with my form params seems to blank out the parent_id. Is this expected behaviour because move_to_child_of sets the parent_id or is this something I screwed up in my implementation? I can grab the parent_id before calling new(), but I’m curious as to the workings going on here. Perhaps there is a better way to handle fetching the parent record in this case…

    Thanks again.

    Nate Nate December 25, 2008 at 03:01 AM

Speak your mind:

*

*


* I hate spam and will never sell or publish your email address.

(You may use textile in your comments.)

Subscribe

Browse by Tag