Browse files

Failing test: create child in after_create callback

Creating a child in a after_* callback results in the child's ancestry
column containing only the parent id, and not the entire parent ancestry
path.
  • Loading branch information...
1 parent 770f2e3 commit 65786f0e85869fb5ba1783c7cd0c8510812cc75d Jason Stirk committed Feb 24, 2011
Showing with 17 additions and 0 deletions.
  1. +17 −0 test/has_ancestry_test.rb
View
17 test/has_ancestry_test.rb
@@ -448,6 +448,23 @@ def test_node_creation_though_scope
end
end
+ # NOTE: A workaround for this case is to call self.reload before calling self.children in the after_create
+ def test_node_creation_in_after_create
+ AncestryTestDatabase.with_model do |model|
+ children=[]
+ model.instance_eval do
+ attr_accessor :idx
+ self.after_create do
+ children << self.children.create!(:idx => self.idx - 1) if self.idx > 0
+ end
+ end
+ node = model.create!(:idx => 3)
+ # In the error case, the ancestry on each item will only contain the parent's id,
+ # and not the entire ancestry tree.
+ assert_equal '1/2/3', children.first.ancestry
+ end
+ end
+
def test_validate_ancestry_exclude_self
AncestryTestDatabase.with_model do |model|
parent = model.create!

0 comments on commit 65786f0

Please sign in to comment.