I figure it out for myself. add_child updates the tables directly, so
objects that have been persisted lose sync with the database. A call to
reload does the trick.
Thanks
_____
From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Soren Telfer
Sent: Wednesday, October 12, 2005 12:41 AM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: [Rails] Help with act_as_nested_set
Hi, I''m trying to get act_as_nested_set working, and I''m
puzzled.
I googled the following
http://codefetch.com/cache?url=http://media.pragprog.com/titles/rails/code/r
ails-code.tgz
<http://codefetch.com/cache?url=http://media.pragprog.com/titles/rails/code/
rails-code.tgz&path=rails-code/ar/acts_as_nested_set.rb&lang=ruby&qy=ruby>
&path=rails-code/ar/acts_as_nested_set.rb&lang=ruby&qy=ruby
Which I''m inserting here:
class Category < ActiveRecord::Base
acts_as_nested_set
end
Category.delete_all
def add_child(parent, name)
child = Category.create(:name => name)
parent.add_child(child) || fail("Couldn''t add #{name}")
child
end
root = Category.create(:name => "Books")
fiction = add_child(root, "Fiction")
non_fiction = add_child(root, "Non Fiction")
add_child(non_fiction, "Science")
add_child(non_fiction, "History")
computers = add_child(non_fiction, "Computers")
add_child(computers, "Software")
add_child(computers, "Hardware")
add_child(fiction, "Mystery")
add_child(fiction, "Romance")
add_child(fiction, "Science Fiction")
def display_children(list)
puts list.map {|child| child.name + "[#{child.id}]" }.join(",
")
end
display_children(root.direct_children) # Fiction, Non Fiction
display_children(non_fiction.direct_children) #Science[129], History[130],
Computers[131]
display_children(non_fiction.all_children) #Romance[135], Science
Fiction[136]
display_children(non_fiction.full_set) #Romance[135], Science
Fiction[136], Non Fiction[128], Science[129]
These
display_children(non_fiction.all_children) #Romance[135], Science
Fiction[136]
display_children(non_fiction.full_set) #Romance[135], Science
Fiction[136], Non Fiction[128], Science[129]
Look just plain wrong. I looked at the table, and it''s set up
correctly:
id | name | parent_id | lft | rgt
-----+-----------------+-----------+-----+-----
100 | Mystery | 93 | 3 | 4
101 | Romance | 93 | 5 | 6
92 | Books | | 1 | 22
95 | Science | 94 | 11 | 12
96 | History | 94 | 13 | 14
98 | Software | 97 | 16 | 17
94 | Non Fiction | 92 | 10 | 21
97 | Computers | 94 | 15 | 20
99 | Hardware | 97 | 18 | 19
93 | Fiction | 92 | 2 | 9
102 | Science Fiction | 93 | 7 | 8
but when I go to use all_children(), I get the for the "Non Fiction"
query,
the query string is
1 = 1 AND (lft > 4) and (rgt < 11)
and it should be lft > 10 and rgt < 21
So, this seems to be some issue with updates and queries. Infact, inside of
all_children(), I have
self.lft: 4
self.class.find(self.id).lft: 10
Such that self != self.class.find(self.id)
I''ve tried inserting save statement, but to no avail.
Need help. Thanks.
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails