# Frustrating Problem - Acts As Tree

**URL:** <https://www.ruby-forum.com/t/frustrating-problem-acts-as-tree/183485>\
**Category:** Rails\
**Created:** [February 15, 2010, 12:29pm UTC](https://www.ruby-forum.com/t/frustrating-problem-acts-as-tree/183485 "2010-02-15T12:29:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [February 15, 2010, 12:29pm UTC](https://www.ruby-forum.com/t/frustrating-problem-acts-as-tree/183485/1 "2010-02-15T12:29:20Z")

</div>

I am a ruby on Rails Novice and I am having an extremely frustrating  
problem - not sure whether acts\_as\_tree plug in is causing this or not  
(or me lacking a fundamental misunderstanding of rails)

I have the following table schema:

create\_table “destinations”, :force =\> true do |t|  
t.string “name”  
t.string “description”  
t.integer “commissions\_rate”,  
:default =\> 0  
t.integer “destinations\_count”,  
:default =\> 0  
t.integer “position”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end

and model def:

class Destination \< ActiveRecord::Base  
attr\_accessible :id, :name, :parent\_id, :centre\_longitude,  
:centre\_latitude, :commissions\_rate, :position, :startzoom,  
:description, :destinations\_count

acts\_as\_tree :counter\_cache =\> true

end

I am trying to adjust the update method in my controller so that if the  
parent commissions\_rate is modified the “children” records of the parent  
are automatically updated with this value, code as follows:

def update  
@destination = Destination.find(params[:id])  
@rate = @destination.commissions\_rate || 0  
@children = @destination.children  
if (@destination.destinations\_count \> 0)  
for child in @children do  
child.commissions\_rate = @rate  
end  
end  
respond\_to do |format|  
if @destination.update\_attributes(params[:destination])  
flash[:notice] = ‘Destination was successfully updated.’  
…  
end

The form only send data for the parent model (not using nested model)

Everything works when I test the above code in the Rails console however  
when running the server the children are failing to have the  
commission\_rate variable updated.  
Would appreciate any help anyone can provide me.

Thanks

David

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [February 15, 2010, 12:41pm UTC](https://www.ruby-forum.com/t/frustrating-problem-acts-as-tree/183485/2 "2010-02-15T12:41:38Z")

</div>

On 15 February 2010 10:46, David K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am a ruby on Rails Novice and I am having an extremely frustrating problem - not sure whether acts\_as\_tree plug in is causing this or not (or me lacking a fundamental misunderstanding of rails)
> 
> Everything works when I test the above code in the Rails console however when running the server the children are failing to have the commission\_rate variable updated.  
> Would appreciate any help anyone can provide me.

You’re not saving the child after you update it’s value

```
for child in @children do
  child.commissions_rate = @rate
  child.save
end

```

or

```
for child in @children do
  child.update_attribute(:commissions_rate, @rate)
end

```

.update\_attribute automatically saves, check the Rails API docs for  
more info on using this method.

PS You’re using a lot of instance variables there, which seem like  
there would be better scoped as local variables (ie drop the @ from  
the variables unless you need them to be accessible on an instance of  
the object - @destination is probably the only legitimate instance  
variable)

---

<div class="post-metadata">

**Author:** ![ntk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@ntk](https://www.ruby-forum.com/u/ntk)\
**Post date:** [February 15, 2010, 1:46pm UTC](https://www.ruby-forum.com/t/frustrating-problem-acts-as-tree/183485/3 "2010-02-15T13:46:02Z")

</div>

Many thanks Michael, it solved the problem, and thanks for the extra  
advice I need it! as where I am based (Indonesia) no one teaches  
rails.
