# Acts\_as\_tree - parent reference is outdated?

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-parent-reference-is-outdated/116367>\
**Category:** Rails\
**Created:** [October 4, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/acts-as-tree-parent-reference-is-outdated/116367 "2007-10-04T21:40:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [October 4, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/acts-as-tree-parent-reference-is-outdated/116367/1 "2007-10-04T21:40:32Z")

</div>

I’m experiencing some unexpected weirdness with acts\_as\_tree.

class Category \< ActiveRecord::Base  
acts\_as\_tree  
has\_many :variations  
end

def variations

# some code that returns the parent category variations if we have

none  
end

> > pc = Category.new  
> > pc.variations \<\< “foo”  
> > pc.save  
> > cc = Category.new  
> > cc.parent = pc  
> > cc.save

So far so good …

> > pc.variations … [“foo”]  
> > cc.variations … [“foo”]

pc.variations.clear

> > pc.variations … [] \<— that’s expected  
> > cc.variations … [“foo”] \<-- UNEXPECTED!!  
> > cc = Category.find 2  
> > cc.variations … [] \<-- again, expected

What’s going on here? The reference to pc and cc.parent are also not  
equal (before reloading even) but I guess that’s just how AR works.  
I’m used to Hibernate returning the same reference.

Any suggestions or explanations?

TIA,

Sean

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [October 5, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/acts-as-tree-parent-reference-is-outdated/116367/2 "2007-10-05T13:33:46Z")

</div>

> What’s going on here? The reference to pc and cc.parent are also not  
> equal (before reloading even) but I guess that’s just how AR works.  
> I’m used to Hibernate returning the same reference.
> 
> Any suggestions or explanations?
> 
> TIA,
> 
> Sean

there must be a problem with your variations method. doublecheck that  
area.

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [October 5, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/acts-as-tree-parent-reference-is-outdated/116367/3 "2007-10-05T15:51:57Z")

</div>

Are you sure its not just that the reference to parent is out-dated?  
Maybe this is just a feature of ActiveRecord? If belongs\_to type  
relationships are just copies of what’s in the database at the time  
that would explain it. It would also explain why re-retrieving the  
record produces the expected result.

Sean

On Oct 5, 7:33 am, Francis S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [October 5, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/acts-as-tree-parent-reference-is-outdated/116367/4 "2007-10-05T16:09:21Z")

</div>

schof wrote:

> Are you sure its not just that the reference to parent is out-dated?  
> Maybe this is just a feature of ActiveRecord? If belongs\_to type  
> relationships are just copies of what’s in the database at the time  
> that would explain it. It would also explain why re-retrieving the  
> record produces the expected result.
> 
> Sean
> 
> On Oct 5, 7:33 am, Francis S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

reload the parent of cc first.

pc.variations.clear  
pc.variations … [] \<— that’s expected  
cc.parent.reload  
cc.variations
