On Aug 26, 1:15 am, Maz <[EMAIL PROTECTED]> wrote:
> To make it easier to test, I wrote a self containing script, with in
> memory sqlite database.
>
> require 'rubygems'
> require 'sequel'
>
> DB = Sequel.sqlite
>
> DB.create_table! :trees do
> primary_key :id
> interger :parent_id, :table => :trees
> text :label
> end
>
> trees = DB[:trees]
> toplevel_id = trees.insert(:label => "1")
> second_level_a_id = trees.insert(:parent_id => toplevel_id, :label =>
> "1:1")
> second_level_b_id = trees.insert(:parent_id => toplevel_id, :label =>
> "1:2")
> third_level_a_id = trees.insert(:parent_id =>
> second_level_a_id, :label => "1:1:1")
>
> puts "********** Table"
> trees.print
>
> class Tree < Sequel::Model
> many_to_one :parent
> one_to_many :children, :class => Tree, :key => :parent_id
> end
>
> puts "********** Children of toplevel"
> Tree[1].children.each do |child|
> p child.label
> end
> puts "********** Children of toplevel through eager"
> Tree.filter(:id => 1).eager(:children).all do |child|
> p child.label
> end
>
> It seems there is a problem with eager loading, or I didn't understand
> the documentation.
Here's the script that works, including the loading of all
descendants, showing you what SQL it is sending to the database when
eager loading:
require 'rubygems'
require 'sequel'
require 'logger'
DB = Sequel.sqlite
DB.create_table! :trees do
primary_key :id
foreign_key :parent_id, :trees
text :label
end
trees = DB[:trees]
toplevel_id = trees.insert(:label => "1")
second_level_a_id = trees.insert(:parent_id => toplevel_id, :label =>
"1:1")
second_level_b_id = trees.insert(:parent_id => toplevel_id, :label =>
"1:2")
third_level_a_id = trees.insert(:parent_id =>
second_level_a_id, :label => "1:1:1")
puts "********** Table"
trees.print
class Tree < Sequel::Model
many_to_one :parent, :class=>:Tree
one_to_many :children, :class=>:Tree, :key => :parent_id
many_to_one :descendants, :read_only=>true, :eager_loader=>(proc do |
key_hash, nodes, associations|
id_map = {}
nodes.each do |n|
n.associations[:children] = []
id_map[n.pk] = n
end
Tree.filter(:parent_id=>id_map.keys).eager(:descendants).all
do |node|
parent = id_map[node.parent_id]
node.associations[:parent] = parent
parent.associations[:children] << node
end
end)
end
DB.loggers << Logger.new($stdout)
puts "********** Children of toplevel"
Tree[1].children.each do |child|
p child.label
end
puts "********** Descendants of toplevel through eager"
Tree.filter(:id => 1).eager(:descendants).all do |root|
prok = proc do |x|
p [(x.parent.label if x.parent), x.label]
x.children.each(&prok)
end
prok.call(root)
end
Here's the output of the script:
********** Table
+--+---------+-----+
|id|parent_id|label|
+--+---------+-----+
| 1| |1 |
| 2| 1|1:1 |
| 3| 1|1:2 |
| 4| 2|1:1:1|
+--+---------+-----+
********** Children of toplevel
I, [2008-08-26T08:08:37.529352 #32096] INFO -- : SELECT * FROM
`trees` WHERE (`id` = 1) LIMIT 1
I, [2008-08-26T08:08:37.531149 #32096] INFO -- : SELECT * FROM
`trees` WHERE (`trees`.`parent_id` = 1)
"1:1"
"1:2"
********** Descendants of toplevel through eager
I, [2008-08-26T08:08:37.533487 #32096] INFO -- : SELECT * FROM
`trees` WHERE (`id` = 1)
I, [2008-08-26T08:08:37.535199 #32096] INFO -- : SELECT * FROM
`trees` WHERE (`parent_id` IN (1))
I, [2008-08-26T08:08:37.539441 #32096] INFO -- : SELECT * FROM
`trees` WHERE (`parent_id` IN (2, 3))
I, [2008-08-26T08:08:37.541501 #32096] INFO -- : SELECT * FROM
`trees` WHERE (`parent_id` IN (4))
[nil, "1"]
["1", "1:1"]
["1:1", "1:1:1"]
["1", "1:2"]
Jeremy
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---