On Aug 19, 3:31 pm, jhaagmans <jaap.haagm...@gmail.com> wrote: > On 19 aug, 19:35, Marnen Laibow-Koser <mar...@marnen.org> wrote: > > > On Aug 19, 1:10 pm, jhaagmans <jaap.haagm...@gmail.com> wrote: > > > > Thank you Marnen, I've made it work using acts_as_tree, which can do > > > everything I was looking for in combination with acts_as_list. > > > NO. STOP. BAD IDEA. > > > Now that I've got your attention, please take a moment and read over > > this thread, particularly the part where I explained that nested sets > > can do what acts_as_tree cannot: get all descendants (no matter how > > deep) with a single database query. There is no way to do that at all > > with acts_as_tree. > > Now look at the example I gave here earlier in this thread. I've used > it and it generates exactly as many queries as my acts_as_tree (which > is based on his example). Not one less. So, what's that guy doing > wrong? I can't figure it out and yes, I've looked into the rdoc.
I finally looked at the example you provided, and I can see why you're confused. That's a *horrible* example. This guy is using awesome_nested_set as if it were acts_as_tree: only going one level into the tree with each query (which is why he's calling @category.children each time). Doing it that way, you're absolutely right that there's no real advantage over acts_as_tree But that's not how awesome_nested_set should be used for maximum benefit. What you should be doing is something like @root.descendants . That will -- with a single query -- load *all* descendants of @root, to infinite depth, into memory. Then you can play with them in memory without touching the database again until you need to save something. [...] > Okay, but again: why? The same story as the previous one? If I can do > all this with one query, I would like to know how. See above. Try methods like descendants, watch the SQL, and observe the real power of the data stricture. > And I would like to > know how I can get the sortable functionality to work with > acts_as_nested_set. You said I should use acts_as_list, but doesn't > that make the lft and rgt columns obsolete? Not really. acts_as_list (at least, the way I use it with awesome_nested_set) only deals with ordering among siblings. lft and rgt, however, contain information that also deals with parent/child relationships. However, you may not need acts_as_list, since unlike acts_as_tree, siblings in nested-set trees *do* have an implied ordering. > And then again, what's the > advantage of using acts_as_nested_set? It describes the tree structure. acts_as_list explicitly does not. [...] > I'll look into it, but chances are I'm not getting this to work with > the scarce information available on the net. It's not even that difficult. Just spend your time on understanding the concept rather than looking for examples. (That said, if I find a good example, I'll let you know.) [...] > > No. awesome_nested_set should not make your controller or views any > > harder to understand. If anything, it should make them *easier* to > > understand, because you do not have to do lots of database queries to > > retrieve the records you need. > > Then I've caught a bad example. The problem is: it's the -only- real > example I could find! And I really need examples to figure these kinds > of things out! As I said above, you've caught a *terrible* example. Drop it and start from scratch. Look at the rdoc again, in particular the README and the InstanceMethods page. Don't be afraid to check out the source either; it's very clearly written. You'll be using the children and descendants methods a lot; these do what you'd expect from their names. Best, -- Marnen Laibow-Koser http://www.marnen.org mar...@marnen.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---