Hi, I''m working with acts_as_tree to print an unordered list of items: Root -- Child ---- Child I''ve got this working, however I''d like to know if there is a more elegant fashion that I''m unaware of: #Takes a parent category and recursivley returns all children def find_all_subcategories(category) if category.children.size > 0 ret = "<ul>" category.children.each { |subcat| if subcat.children.size > 0 ret += "<li>" + subcat.title ret += find_all_subcategories(subcat) ret += "</li>" else ret += "<li>" + h(subcat.title) + "</li>" end } ret += "</ul>" end end Thanks, -Justin Palmer Encytemedia.com
Justin, I don''t know about the acts_as_nested_set that ships with Rails, but I think that a decent implementation would allow you to do this without multiple passes to the database. - Sam On May 17, 2005, at 1:36 AM, Justin Palmer wrote:> Hi, > I''m working with acts_as_tree to print an unordered list of items: > Root > -- Child > ---- Child > > I''ve got this working, however I''d like to know if there is a more > elegant fashion that I''m unaware of: > > #Takes a parent category and recursivley returns all children > def find_all_subcategories(category) > if category.children.size > 0 > ret = "<ul>" > category.children.each { |subcat| > if subcat.children.size > 0 > ret += "<li>" + subcat.title > ret += find_all_subcategories(subcat) > ret += "</li>" > else > ret += "<li>" + h(subcat.title) + "</li>" > end > } > ret += "</ul>" > end > end > > Thanks, > -Justin Palmer > Encytemedia.com > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On May 17, 2005, at 2:47 AM, Sam Goldman wrote:> I don''t know about the acts_as_nested_set that ships with Rails, > but I think that a decent implementation would allow you to do this > without multiple passes to the database.Google for "Adjacency Lists" to see how you can store tree data in SQL that lets you do a single (simple) query that will gather everything you need to draw any part of the subtree: http://www.google.com/search?q=%22adjacency+list%22+sql
On May 17, 2005, at 6:46 AM, Gavin Kistner wrote:> On May 17, 2005, at 2:47 AM, Sam Goldman wrote: >> I don''t know about the acts_as_nested_set that ships with Rails, >> but I think that a decent implementation would allow you to do >> this without multiple passes to the database. > > Google for "Adjacency Lists" to see how you can store tree data in > SQL that lets you do a single (simple) query that will gather > everything you need to draw any part of the subtree:Er, while that search term does get you some good hits, "Adjacency List" is actually the description of the normal/simple/stupid way to store a hierarchy in a SQL table. "Nested Set" is the impressive, effective way to store a tree. http://www.google.com/search?q=%22adjacency+list%22+%22nested+set%22+sql Sorry for the extra noise. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 17/05/2005, at 3:36 PM, Justin Palmer wrote:> I''ve got this working, however I''d like to know if there is a more > elegant fashion that I''m unaware of:No don''t think there is... not with the standards acts_as_tree anyways. If you''re looking at doing many of these then definitely try out nested set. We moved our model over to nested set quite painlessly and it''s served us well. - tim lucas aviditybytes.com
Be advised that nested sets are very expensive to update. Only use them when you have significant more reads then writes. On 5/17/05, Tim Lucas <t.lucas-l/qNJNvq70OzaBltdDZI6w@public.gmane.org> wrote:> On 17/05/2005, at 3:36 PM, Justin Palmer wrote: > > I''ve got this working, however I''d like to know if there is a more > > elegant fashion that I''m unaware of: > > No don''t think there is... not with the standards acts_as_tree anyways. > > If you''re looking at doing many of these then definitely try out nested > set. We moved our model over to nested set quite painlessly and it''s > served us well. > > - tim lucas > > aviditybytes.com > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
> If you''re looking at doing many of these then definitely try out > nested set. We moved our model over to nested set quite painlessly and > it''s served us well.Nested set is extremely effective, you just need to abstract it so you don''t ff up the coordinates when you are modifying the hierarchy :) I like it. _a -- alex black, founder the turing studio, inc. 510.666.0074 root-16h2cdTTKgpzNNFeSAH1EA@public.gmane.org http://www.turingstudio.com 2600 10th street, suite 635 berkeley, ca 94710
Rails alredy supports nested sets: See acts_as_nested_set http://api.rubyonrails.com/classes/ActiveRecord/Acts/NestedSet/ClassMethods.html http://api.rubyonrails.com/classes/ActiveRecord/Acts/NestedSet/ClassMethods.html Works perfectly. And Sam Goldman: I looked at your approach. I''m trying to come up with something that only calls the database once to print the whole tree. Problem is I''m new to Ruby. In PHP I did this with a recursive function, similar to how you did it, but without multiple DB calls (by getting to whole tree with one DB call and then iterating through it with PHP - not the best approach either but at least saves DB calls). Thanks for the code though! Helps me learn ruby - Rob> Er, while that search term does get you some good hits, "Adjacency > List" is actually the description of the normal/simple/stupid way to > store a hierarchy in a SQL table. "Nested Set" is the impressive, > effective way to store a tree.
While the big-O complexity is obviously greater, I remember reading somewhere that in actual usage the difference ended up being negligible (obviously for a particular use case). It would be wise to benchmark both cases if speed is a factor. - Sam On May 17, 2005, at 3:30 PM, Tobias Luetke wrote:> Be advised that nested sets are very expensive to update. Only use > them when you have significant more reads then writes. > > On 5/17/05, Tim Lucas <t.lucas-l/qNJNvq70OzaBltdDZI6w@public.gmane.org> wrote: > >> On 17/05/2005, at 3:36 PM, Justin Palmer wrote: >> >>> I''ve got this working, however I''d like to know if there is a more >>> elegant fashion that I''m unaware of: >>> >> >> No don''t think there is... not with the standards acts_as_tree >> anyways. >> >> If you''re looking at doing many of these then definitely try out >> nested >> set. We moved our model over to nested set quite painlessly and it''s >> served us well. >> >> - tim lucas >> >> aviditybytes.com >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > > -- > Tobi > http://www.snowdevil.ca - Snowboards that don''t suck > http://www.hieraki.org - Open source book authoring > http://blog.leetsoft.com - Technical weblog > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >