# Acts\_as\_tree "soft destroy", ie. "move up" children

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-soft-destroy-ie-move-up-children/120316>\
**Category:** Rails\
**Created:** [November 15, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/acts-as-tree-soft-destroy-ie-move-up-children/120316 "2007-11-15T19:04:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 15, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/acts-as-tree-soft-destroy-ie-move-up-children/120316/1 "2007-11-15T19:04:51Z")

</div>

By default, when you destroy a parent node all of the children nodes  
are destroyed as well.

Is there a way I can modify this so that when you destroy a parent  
node all the children nodes are simply “moved up” within the tree?

Thanks!

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 15, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/acts-as-tree-soft-destroy-ie-move-up-children/120316/2 "2007-11-15T19:25:21Z")

</div>

come to think of it, I should probably just create my own function  
called destroy\_and\_move\_up or something =P

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 15, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/acts-as-tree-soft-destroy-ie-move-up-children/120316/3 "2007-11-15T19:35:42Z")

</div>

put it in the before\_destroy callback of the model

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 18, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/acts-as-tree-soft-destroy-ie-move-up-children/120316/4 "2007-11-18T17:47:33Z")

</div>

> > put it in the before\_destroy callback of the model

I tried using the before\_destroy for a similar purpose - I wanted to  
stop deletion if the node has children. But I ran into trouble, the  
children were destroyed anyway. From what I have read elsewhere in  
this group, I believe the before\_destroy is only called after  
dependent\_destroy is actioned. That may be what is happening here.

Seems to me to write a seperate method may be the way to go.

Tonypm

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 15, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/acts-as-tree-soft-destroy-ie-move-up-children/120316/5 "2007-11-15T23:17:11Z")

</div>

I tried that already.

def before\_destroy  
self.children.each {|child|  
child.parent\_id = nil  
child.save  
}  
end

(Yeah, I know, it doesn’t “move them up”, it just sets them to new  
root nodes)

However, rails won’t let me do that. It says I can’t modify a frozen  
hash. I’m not sure what that means, but I assume it means I can’t  
modify things that are about to be destroyed.

On Nov 15, 1:35 pm, Thorsten M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
