Acts_as_nested_set ActiveRecord rendered with mx:Tree in Flex.

ActiveRecord: app/models/category.rb

app/models/category.rb
class Category < ActiveRecord::Base acts_as_nested_set end

Controller: app/controllers/categories_controller.rb

app/controllers/categories_controller.rb
class CategoriesController < ApplicationController def index Category.result_to_attributes_xml(Category.root.full_set) end end

Flex Application: ActsAsNestedSet.mxml

ActsAsNestedSet.mxml
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationComplete="categories.send()"> <mx:HTTPService id="categories" url="http://localhost:3000/categories" resultFormat="e4x" /> <mx:Tree dataProvider="{categories.lastResult}" labelField="@name" width="100%" height="100%" /> </mx:Application>

Result:
20071123_categories.jpg

XML generated by Category.result_to_attributes_xml(Category.root.full_set):

XML generated by Category.result_to_attributes_xml(Category.root.full_set)
<node name="Main Category" id="15" description=""> <node name="Cameras &amp; Photo" id="16" description=""> <node name="Bags" id="17" description=""/> <node name="Accessories" id="18" description=""/> <node name="Analog Cameras" id="19" description=""/> <node name="Digital Cameras" id="20" description=""/> </node> <node name="Cell Phones" id="21" description=""> <node name="Accessories" id="22" description=""/> <node name="Phones" id="23" description=""/> <node name="Prepaid Cards" id="24" description=""/> </node> <node name="Dvds" id="25" description=""> <node name="Blueray" id="26" description=""/> <node name="HD DVD" id="27" description=""/> <node name="DVD" id="28" description=""/> </node> </node>

I used the http://wiki.rubyonrails.org/rails/pages/BetterNestedSet plugin.

Too cool!

UPDATE: The BetterNestedSet plugin doesn’t work out of the box with Rails 2.0 RC1. Thanks Joel for that info. Read more in the comment of this blog entry.

UPDATE2: Thanks Fabien, BetterNestedSet now works with Rails 2.0!

Posted by Daniel Wanja Sat, 24 Nov 2007 03:30:13 GMT


Comments

  1. Joel AZEMAR 2 days later:

    Just be careful if you use rails 2.0 RC1 because the BetterNestedSet plugin dosen’t work. ArgumentError (wrong number of arguments (2 for 1)): Aimeric PIETERS in official BetterNestedSet plugin forum write : for fix that open /Library/Ruby/Gems/1.8/gems/activerecord-1.99.0/lib/active_record/
    base.rb, line 2029, and change “attributes_with_quotes(false, false)” by “attributes_with_quotes(false)”.

  2. Daniel Wanja 2 days later:

    Thanks for that info. In fact I bumped into this one too and worked around it opening better_nested_set.rb and modifying the private attributes_with_quotes method and adding a default second parameter to the method signature as follows:

    def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true)

    I haven’t fully tested this change so I didn’t provide it back to the symetrie guys. But it works pretty well for my project.

  3. Fabien 3 days later:

    Please checkout the trunk release of BetterNestedSet – I’ve just changed attributes_with_quotes to be in line with ActiveRecord trunk. Enjoy…

  4. Daniel T Chan 13 days later:

    I follow this sample – the better_nested_set approach, with the models :

    acts_as_nested_set :parent_column => “intParent”, :left_column => “lft”, :right_column => “rgt” self.primary_key = “intId”

    and follow the controller approach to call result_to_attributes_xml. However, I end up :

    undefined method `parent_id’ for #{Attribldr4s:0xb7579448}

    METHOD_MISSING error.

    The application trace is as follows:

    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/base.rb:1863:in `method_missing’

    /var/www/attredit/current/attredit/vendor/plugins/betternestedset/lib/better_nested_set.rb:264:in `result_to_attributes_xml’

    /var/www/attredit/current/attredit/vendor/plugins/betternestedset/lib/better_nested_set.rb:264:in `select’

    /var/www/attredit/current/attredit/vendor/plugins/betternestedset/lib/better_nested_set.rb:264:in `result_to_attributes_xml’

    /var/www/attredit/current/attredit/app/controllers/info4_controller.rb:4:in `index’

    I wonder if any one can help?

    thank you

  5. Dan 7 months later:

    Nice post. I’m also using better nested set for a simple classification project that also exposes data (just like above) to a Flex tree component. However, I did notice that a ton of select count(*) statements get executed when I retrieve the entire grid (one for each entry, I believe). Do you see the same thing? Like I said if you change Category to Commodity, we have the same exact scenario.

  6. Dan 7 months later:

    Nice post. I’m also using better nested set for a simple classification project that also exposes data (just like above) to a Flex tree component. However, I did notice that a ton of select count(*) statements get executed when I retrieve the entire grid (one for each entry, I believe). Do you see the same thing? Like I said if you change Category to Commodity, we have the same exact scenario.

  7. Nick G 9 months later:

    Nested sets are a great way to speed up information retrieval.
    Great article !

    free Rapidshare premium account

  8. tom about 1 year later:

    cant figure out how to use the action:
    result_to_attributes_xml. when i copy it into the controller, it stil complains about errors, etc. can someone please give me a headsup?

    thx

  9. pete about 1 year later:

    im having as well trouble…is there a stepbystep guide how to use this? thx

  10. Daniel Wanja about 1 year later:

    I also encountered issue with better nested set using Rails 2.1 and 2.2, so maybe that’s you problem. Check out awesome_nested_set, I blogged about it http://onrails.org/articles/2008/08/19/nested-to_xml-for-awesome_nested_set and have some sample apps that use the awesome nested set on github (http://github.com/danielwanja/flexonrails/tree/102a32e079f8153fa5f07fe495e8e59f9d8a0af4/18_Advanced_Data_Grid_and_Awesome_Nested_Sets)