On 21 aug, 07:32, jhaagmans <[email protected]> wrote:
> On 21 aug, 13:16, Felix Schäfer <[email protected]> wrote:
>
>
>
>
>
> > Am 21.08.2009 um 12:19 schrieb jhaagmans:
>
> > > @attributes = root_attribute.self_and_descendants
>
> > > However, when I put the following in my views to be able to descend
> > > into the model:
>
> > > <% @attributes.root.children.each do |attribute| %>
> > > ...
> > > <% end %>
>
> > I think I now see where the problem is. To be quite honest, I'm
> > surprised this even works, because @attributes is a set, and you
> > shouldn't be able to call root on it.
Yeah, that shouldn't work at all. According to the rdoc, Model#root
should only be defined as an instance method on Model objects.
> > Anyway, self_and_descendants
> > gets you a flat set, from which you could reconstruct a tree on your
> > own, but I don't think that would be that easy.
It's not easy -- I've done it -- but it's more efficient, and probably
the right thing to do for most use cases. Basically, you determine
the level of each node, and go from there.
Jaap, as you've described your use case elsewhere as being a
progressive drilldown, you may actually want to use the less efficient
structure here -- I'm not sure. However, using nested sets will mean
that you're not stuck with that through your whole application.
>
> > What I can come up right now is a way to cut the calls down to the
> > number of nodes with children with something like:
>
> > """
> > def my_awesome_helper(root)
> > result = "<ul>" + "<li>" + root.someattribute
> > if root.children {
> > root.children.each do |child|
> > result += my_awesome_helper(child)
> > end
> > end
> > result += "</li>" + "</ul>"
> > end
> > """
> > (yeah, ugly, and I'm not even sure the syntax is right, but you get
> > the idea)
>
> > You'd then just have to call the helper on the root node in your view.
>
> > Mit freundlichen Grüßen,
>
> > Felix Schäfer
>
> Danke Felix!
>
> I'm quite unsure whether this would limit the number of queries.
Instead of posting that you're "quite unsure" whether this would work,
try it and find out.
> I
> already call children.each in my views, so this would probably, in
> terms of performance, won't really help me.
I think it will help, if you've already fetched the whole set. I
believe (but am not absolutely certain) that the tree is cached, so
once you do @root.descendants, the whole thing is fetched, and
@root.children[0].children will not generate another query. Again,
try it and watch the SQL.
> I was told I could perform
> this using one single query, but that seems impossible.
What's impossible about it?
> And that
> brings me back to a point where I question the advantages of using
> awesome_nested_set over using acts_as_tree in combination with
> acts_as_list.
At this point, you're going around in circles. Stop theorizing and
actually write some code.
>
> The thing is, it all made sense! And it would have been perfect.
So try it!
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---