# ActiveRecord: strange tree behavior?

**URL:** <https://www.ruby-forum.com/t/activerecord-strange-tree-behavior/88745>\
**Category:** Rails\
**Created:** [March 14, 2007, 12:11am UTC](https://www.ruby-forum.com/t/activerecord-strange-tree-behavior/88745 "2007-03-14T00:11:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![siegi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@siegi](https://www.ruby-forum.com/u/siegi)\
**Post date:** [March 14, 2007, 12:11am UTC](https://www.ruby-forum.com/t/activerecord-strange-tree-behavior/88745/1 "2007-03-14T00:11:01Z")

</div>

hi,

can someone help me with understanding, why i can’t get my  
set\_sub\_tree method to run correctly and what to do to make it work?

when i run the testcase below i get

* * *

D:\ruby\test\>ruby test/unit/demo\_test.rb  
Loaded suite test/unit/demo\_test  
Started  
F  
Finished in 0.625 seconds.

1. Failure:  
test\_set\_sub\_tree(DemoTest) [test/unit/demo\_test.rb:13]:  
fails@4.  
\<200\> expected but was  
\<100\>.

## 1 tests, 4 assertions, 1 failures, 0 errors

why does it fail at all? why does it fail@4 but not fail@2 ???  
am i using save incorrectly?

what do i need to make it work? what haven’t i read so i do not  
understand the problem?

many thanx in advance,  
siegi

## FILE demo.rb:

class Demo \< ActiveRecord::Base  
acts\_as\_tree

def set\_sub\_tree(value)  
self.value = value  
self.save!

```
children.each { |child|
  child.set_sub_tree(value)
}

```

end  
end

## FILE 001\_create\_demos.rb:

class CreateDemos \< ActiveRecord::Migration  
def self.up  
create\_table :demos do |t|  
t.column “value”, :integer  
t.column “parent\_id”, :integer  
end  
end

def self.down  
drop\_table :demos  
end  
end

## FILE demos.yml:

one:  
id: 1  
value: 0  
two:  
id: 2  
value: 0  
parent\_id: 1

## FILE demo\_test.rb:

require File.dirname( **FILE** ) + ‘/…/test\_helper’

class DemoTest \< Test::Unit::TestCase  
fixtures :demos

def test\_set\_sub\_tree  
demos(:one).set\_sub\_tree(100)  
assert\_equal 100, demos(:one).value, “fails@1”  
assert\_equal 100, demos(:two).value, “fails@2”

```
demos(:one).set_sub_tree(200)
assert_equal 200, demos(:one).value, "fails@3"
assert_equal 200, demos(:two).value, "fails@4"

```

end  
end
