# How do I get act\_as\_tree members to use the same object for

**URL:** <https://www.ruby-forum.com/t/how-do-i-get-act-as-tree-members-to-use-the-same-object-for/106598>\
**Category:** Rails\
**Created:** [June 12, 2007, 10:20am UTC](https://www.ruby-forum.com/t/how-do-i-get-act-as-tree-members-to-use-the-same-object-for/106598 "2007-06-12T10:20:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Stephens](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Ben\_Stephens](https://www.ruby-forum.com/u/Ben_Stephens)\
**Post date:** [June 12, 2007, 10:20am UTC](https://www.ruby-forum.com/t/how-do-i-get-act-as-tree-members-to-use-the-same-object-for/106598/1 "2007-06-12T10:20:06Z")

</div>

Hi, how do I get act\_as\_tree members to use the same object for their  
parent?

For example at the moment I have:

tree\_member = TreeMember.find(8)  
sibling\_tree\_member = TreeMember.find(9)

tree\_member.parent.id  
=\> 2

sibling\_tree\_member.parent.id  
=\> 2

tree\_member.parent.object\_id  
=\> 37862220

sibling\_tree\_member.parent.object\_id  
=\> 37850860

Regards,  
Ben

---

<div class="post-metadata">

**Author:** ![Ben\_Stephens](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@Ben\_Stephens](https://www.ruby-forum.com/u/Ben_Stephens)\
**Post date:** [June 12, 2007, 5:32pm UTC](https://www.ruby-forum.com/t/how-do-i-get-act-as-tree-members-to-use-the-same-object-for/106598/2 "2007-06-12T17:32:01Z")

</div>

Rails does not guarantee that all reads of a row result in the same  
Ruby object. When you traverse a relationship it will re-read the row  
if that object has not traversed that relationship previously. In  
your case the .parent relationship is being traversed from each child  
object so you get 2 ruby objects for the same row in the database. If  
you find all rows with parent == nil and walk down the tree you may be  
able to ensure that there is only one ruby object for each row. But,  
it may only mean that the children relationship is cached. This is  
the biggest downside of AR and other thin layer OR layers. But, it  
also has advantages in that you can work with record sets that are  
distinct in purpose even though they happen to include the same  
records.

Michael
