Many thanks, I understand how it works now (I hope).
In http://sequel.rubyforge.org/rdoc/files/doc/advanced_associations_rdoc.html
you might want to correct the simple Node model with the ':class
=> :Node'.
Also, after the eager loader ancestors and descendants, you might put
a little explanation along the lines :
===
Taking ':descendants', it will eager load the data when you call it
through ':eager'. It still hands to you only the tuples that you would
get wihtout ':eager'. However, it will preload all descendants before
handing the results to you.
Note also that it will only preload the ones you filtered through the
'Node.filter' in the eager loader.
For example, if Node has a 'color' attribute, and you used :
Node.filter(:parent_id => id_map.keys).exclude(:color =>
'white').eager(:descendants).all do |node|
When you steps through all children, see below, you will only get
descendants that matched, and not all.
Node.filter(:id => 1).eager(:descendants).all do |root|
# Anonymous function which recurses through all children of a given
node
prok = proc do |x|
p [(x.parent.label if x.parent), x.label]
x.children.each(&prok)
end
# Call the anonymous function on the selected node
prok.call(root)
end
===
At least, that's how I undertand it after your example.
I find that the associations with keys which have the same name as the
previous 'one_to_many' and 'many_to_one' is confusing things at first.
If we were to replace, only in the eager loader, 'parent' with
'father', and 'children' with 'sons', then the example would be :
Node.filter(:id => 1).eager(:descendants).all do |root|
# Anonymous function which recurses through all children of a given
node
prok = proc do |x|
p [(x.associations[:father].label if x.associations[:father]),
x.label]
x.associations[:sons].each(&prok)
end
# Call the anonymous function on the selected node
prok.call(root)
end
That would also better reflect the use of xxx.associations which is
used inside the eager loader.
In fact, the close interaction between 'parent', 'children' and
'descendants' did confuse me. Mainly the 'magically' linked
node.associations[:parent], with node.parent. Adding to that, if you
filter out some of the results, they are also excluded from
node.children 'magically'.
I also reread
http://sequel.rubyforge.org/rdoc/classes/Sequel/Model/Associations/EagerLoading.html
but it didn't helped me more.
Overall, I think documentation on associations lacks somewhere between
simple and advanced usage. Also, I hardly ever used ActiveRecord, and
comparing the two is even more confusing.
And last, can I use one_to_many :descendants, instead of many_to_one ?
It seems a lot more like a one_to_many to me, and it doesn't change
the results, in this contrived example.
thanks for the quick reply.
Maz
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-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/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---