# Acts\_as\_tree and before\_save

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-and-before-save/107965>\
**Category:** Rails\
**Created:** [June 27, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/acts-as-tree-and-before-save/107965 "2007-06-27T22:01:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oleg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ebca7d/32.png) [@oleg](https://www.ruby-forum.com/u/oleg)\
**Post date:** [June 27, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/acts-as-tree-and-before-save/107965/1 "2007-06-27T22:01:31Z")

</div>

We all know that acts\_as\_tree gives us ability to set up data in a  
tree-like structure and use methods like ‘parent’, ‘children’ and so on.

Something I’m completely stuck on. Observe.

class Page \< ActiveRecord::Base  
acts\_as\_tree

def before\_save  
raise self.parent.to\_yaml  
end  
end

Well, you’d think that it should show element’s parent element but for  
some reason it’s always blank.

Why don’t I have access to parent element here? Parent method seems to  
work just fine when I actually save the thing. So child\_element =  
element.children.create(blahblah) and child\_element.parent gives me the  
parent element as it suppose to.

What am I missing here?

Thanks.

---

<div class="post-metadata">

**Author:** ![oleg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ebca7d/32.png) [@oleg](https://www.ruby-forum.com/u/oleg)\
**Post date:** [June 28, 2007, 10:03am UTC](https://www.ruby-forum.com/t/acts-as-tree-and-before-save/107965/2 "2007-06-28T10:03:24Z")

</div>

> def before\_save  
> raise self.parent.to\_yaml  
> end  
> end
> 
> Why don’t I have access to parent element here? Parent method seems to  
> work just fine when I actually save the thing. So child\_element =  
> element.children.create(blahblah) and child\_element.parent gives me the  
> parent element as it suppose to.

it doesn’t work, because raise makes it not work. raise is:

[http://www.rubycentral.com/book/ref\_m\_kernel.html#Kernel.raise](http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.raise)

it raises an exception; maybe you’re looking for render or some other  
method, but if i’m getting your drift (maybe i’m missing it entirely)  
raise is not what you’re looking for.

---

<div class="post-metadata">

**Author:** ![oleg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ebca7d/32.png) [@oleg](https://www.ruby-forum.com/u/oleg)\
**Post date:** [June 28, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/acts-as-tree-and-before-save/107965/3 "2007-06-28T14:04:15Z")

</div>

> it doesn’t work, because raise makes it not work. raise is:
> 
> [http://www.rubycentral.com/book/ref\_m\_kernel.html#Kernel.raise](http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.raise)
> 
> it raises an exception; maybe you’re looking for render or some other  
> method, but if i’m getting your drift (maybe i’m missing it entirely)  
> raise is not what you’re looking for.

I know what raise does. Point is that at that stage it should error out  
and show me contents of the parent element. In my case it’s always nil  
because self.parent is nil, instead of being a Page object.

---

<div class="post-metadata">

**Author:** ![oleg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ebca7d/32.png) [@oleg](https://www.ruby-forum.com/u/oleg)\
**Post date:** [June 28, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/acts-as-tree-and-before-save/107965/4 "2007-06-28T15:48:25Z")

</div>

> I know what raise does.

sorry 'bout that. didn’t mean to bring it along that way … 🙂

> and show me contents of the parent element. In my case it’s always nil  
> because self.parent is nil, instead of being a Page object.

…what happens if you do it in an after\_save call? a small guess would  
be it wouldn’t work on a before\_save :on =\> create (whereas :on =\>  
update would work) because the new record doesn’t actually exist in the  
db when the before\_save is called. if i am correct, the child.parent  
method actually goes to the db to check the parent\_id column, and the  
before\_save is called before the actual writing to the db.

…?
