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 features^1, 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.
There’s lots more info in the README on GitHub, so check it out. Let us know if you have any suggestions or feedback.
- It also backports named\_scope for those still on Rails 2.0