ActiveRecord: strange tree behavior?


#1

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