Dustin Frazier
2007-Jan-19 01:02 UTC
[Betternestedset-talk] ez-set and scope, helpful code
I recently discovered BetterNestedSet and this mailing list while looking for a simple way to manage a hierarchy of categories for a project management solution I''m working on. I''m very intrigued by the new ez-set version, since IMHO the interface is much simpler than the previous set of methods. I''m all in favor of making a *better* API rather than trying to match acts_as_tree. One question: Does the concept of scope still apply in ez-set? I tried using it with multiple root nodes and no scope, and it seems to work fine, but I''m not sure if it''s just a coincidence with how I have my initial data setup. I haven''t written much code yet to move nodes around, re-/un-root subtrees, etc. Also, I thought I''d share a bit of migration code that might be useful to others or when you go about writing a step-by-step setup guide. It''s a method I added to my CreateCategories migration class that makes it really easy to create leaf nodes along with all parents up the tree: def self.create_category (path) parent = nil path.split('':'').each do |name| cat = Category.find_or_create_by_name_and_parent_id(name, parent) parent.children << cat if !parent.nil? parent = cat end end I call this from my "up" method to create leaf nodes: create_category(''Infrastructure:Hardware:Servers'') create_category(''Infrastructure:Software:Windows XP'') ... Seems to work, but do let me know if I''m missing some subtly of the new API. Thanks for the great plugin(s)! Keep us posted on the feedback on ez-set... Dustin
Hi Dustin, Thanks for your comments, and we''re glad you''ve been enjoying the plugin.> methods. I''m all in favor of making a *better* API rather than trying to > match acts_as_tree.Actually, the ez-set branch matches the acts_as_tree API much more closely than trunk does.> > One question: Does the concept of scope still apply in ez-set?Yes. All that stuff works the same.> def self.create_category (path) > parent = nil > path.split('':'').each do |name| > cat = Category.find_or_create_by_name_and_parent_id(name, parent) > parent.children << cat if !parent.nil? > parent = cat > end > endHey, that looks neat. If you wanted to you could post it on the wiki page: http://wiki.rubyonrails.org/rails/pages/BetterNestedSet Cheers, Krishna