Acts_as_nested_set ActiveRecord rendered with mx:Tree in Flex. 7

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

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!

Comments

Leave a response

  1. Joel AZEMAR Mon, 26 Nov 2007 09:13:51 GMT

    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 Mon, 26 Nov 2007 13:46:49 GMT

    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 Mon, 26 Nov 2007 19:28:18 GMT

    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 Fri, 07 Dec 2007 00:42:10 GMT

    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 Fri, 13 Jun 2008 01:40:16 GMT

    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 Fri, 13 Jun 2008 01:40:21 GMT

    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 Thu, 21 Aug 2008 23:22:08 GMT

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

    free Rapidshare premium account

Comments