Acts_as_nested_set ActiveRecord rendered with mx:Tree in Flex.
ActiveRecord: app/models/category.rb
app/models/category.rbclass Category < ActiveRecord::Base acts_as_nested_set end
Controller: app/controllers/categories_controller.rb
app/controllers/categories_controller.rbclass 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:

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 & 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!
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)”.
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.
Please checkout the trunk release of BetterNestedSet – I’ve just changed attributes_with_quotes to be in line with ActiveRecord trunk. Enjoy…
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
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.
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.
Nested sets are a great way to speed up information retrieval.
Great article !
free Rapidshare premium account
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
im having as well trouble…is there a stepbystep guide how to use this? thx
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)