# Problems with acts\_as\_tree

**URL:** <https://www.ruby-forum.com/t/problems-with-acts-as-tree/115965>\
**Category:** Rails\
**Created:** [October 1, 2007, 1:44am UTC](https://www.ruby-forum.com/t/problems-with-acts-as-tree/115965 "2007-10-01T01:44:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 1, 2007, 1:44am UTC](https://www.ruby-forum.com/t/problems-with-acts-as-tree/115965/1 "2007-10-01T01:44:04Z")

</div>

I have an acts\_as\_tree association in my application and for the most  
part it’s working fine but I’m concerned specifically about one part  
of the design. When I add children if it is the first child being  
added I want to flag it as the first child object. I chose to do this  
by overriding \<\< so to add children to the parent object I do parent  
\<\< child. This works fine now but if someone were to add to  
object.children manually (parent.children \<\< child) it wouldn’t keep  
track of the first child. What’s the ruby way of handling this? I’m  
thinking maybe I need to override the children method somehow but I’m  
not sure. Thanks any advice would be appreciated. -Mike

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 1, 2007, 2:10am UTC](https://www.ruby-forum.com/t/problems-with-acts-as-tree/115965/2 "2007-10-01T02:10:22Z")

</div>

It depends on how your app works. If you aren’t moving children at  
all, you don’t have to do anything, because the IDs will keep the  
children in order. Otherwise you could register a before\_create or  
before\_save method that checks for other children in the database,  
then sets the flag.

Krishna

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 1, 2007, 5:44am UTC](https://www.ruby-forum.com/t/problems-with-acts-as-tree/115965/3 "2007-10-01T05:44:02Z")

</div>

You could create a before\_save callback. Check to see if the parent  
has any children already. If not, set the flag.
