I moved from acts_as_tree to acts_as_nested_set because I need the
all_children method.
I’ve seen an alternative way to add this method to acts_as_tree here:
http://www.chuckvose.com/articles/2006/05/24/recursive-children
But I’d prefer to use the more efficient way of acts_as_nested_set (and
not to have to revert to my old code).
Anyway, here’s my issue: if I already have a root1 element with lft:1
and rgt:4 (has one child) and add a new root2 (has one child also), it
is assigned lft:1 and rgt:4 also!
So if I ask for root2.all_children I also retrieve the child of root1!
My migration schema:
create_table “elements”, :force => true do |t|
t.column “name”, :string
t.column “category_id”, :integer
t.column “parent_id”, :integer
t.column “lft”, :integer
t.column “rgt”, :integer
t.column “created_at”, :datetime
end
Some code to reproduce the results:
root1 = Element.create(:name => “root1”)
root1.add_child(Element.create(:name => “root1child”)
root2 = Element.create(:name => “root2”)
root2.add_child(Element.create(:name => “root2child”)
Data view:
ID | PARENT_ID | LFT | RGT | NAME
1 | 0 | 1 | 4 | root1
2 | 1 | 2 | 3 | root1child
3 | 0 | 1 | 4 | root2
4 | 1 | 2 | 3 | root2child
Which produces this query when asking for root2.all_children:
SELECT * FROM keywords WHERE (1 = 1 AND (lft > 1) and (rgt < 4))
Baaaad!! I though it would be:
ID | PARENT | LEFT | RIGHT | DATA
1 | 0 | 1 | 4 | root1
2 | 1 | 2 | 3 | child1
1 | 0 | 5 | 8 | root2
2 | 1 | 6 | 7 | child1
And produce:
SELECT * FROM keywords WHERE (1 = 1 AND (lft > 5) and (rgt < 8))
Is this normal behavior for acts_as_nested_set or is there something
broke here?
Thank you for reading me!