# Acts\_as\_tree problem accessing parent object

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-problem-accessing-parent-object/63205>\
**Category:** Rails\
**Created:** [June 13, 2006, 6:08am UTC](https://www.ruby-forum.com/t/acts-as-tree-problem-accessing-parent-object/63205 "2006-06-13T06:08:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 13, 2006, 6:08am UTC](https://www.ruby-forum.com/t/acts-as-tree-problem-accessing-parent-object/63205/1 "2006-06-13T06:08:02Z")

</div>

i have a model called certification\_types that i’ve declared as  
acts\_as\_tree. I have a model called certification that belongs\_to  
:certification\_type .

class CertificationController \< ApplicationController  
…  
def summary\_list  
@certifications = Certification.find(:all, :order=\>‘date’)  
end  
end

summary\_list.rhtml  
\<% for certification in @certifications %\>  
\<%= certification.certification\_type.parent.id %\>  
\<% end %\>

so, the line above (\<%=  
certification.certification\_type.parent.type\_description %\> ) does not  
work; it says The error occured while evaluating nil.type\_description.

if i do a debug(certification.certification\_type.parent) it shows that i  
have a hash of values:  
— !ruby/object:CertificationType  
attributes:  
type\_description: Allergy & Immunology  
id: “12”  
parent\_id: “11”

can anyone help me with how to access the parent type\_description when  
looping through my collection?

Thanks,

Nate

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 13, 2006, 11:23am UTC](https://www.ruby-forum.com/t/acts-as-tree-problem-accessing-parent-object/63205/2 "2006-06-13T11:23:36Z")

</div>

> \<% for certification in @certifications %\>  
> \<%= certification.certification\_type.parent.id %\>  
> \<% end %\>

…my guess is that the reason you are getting nil is because there must  
be cases in the loop that certification does not have a certification  
type - - did you put the  
“debug(certification.certification\_type.parent)” AFTER the loop? , if  
so, you may have gotten the last value of the loop - which does have a  
certification type, but other objects in the loop do not, therefore  
returning a nil object - -  
the best way to see if this is correct or not is to put the debug(…)  
inside the loop and see what it returns…

shai

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 13, 2006, 4:41pm UTC](https://www.ruby-forum.com/t/acts-as-tree-problem-accessing-parent-object/63205/3 "2006-06-13T16:41:11Z")

</div>

shai wrote:

> > \<% for certification in @certifications %\>  
> > \<%= certification.certification\_type.parent.id %\>  
> > \<% end %\>
> 
> …my guess is that the reason you are getting nil is because there must  
> be cases in the loop that certification does not have a certification  
> type - - did you put the  
> “debug(certification.certification\_type.parent)” AFTER the loop? , if  
> so, you may have gotten the last value of the loop - which does have a  
> certification type, but other objects in the loop do not, therefore  
> returning a nil object - -  
> the best way to see if this is correct or not is to put the debug(…)  
> inside the loop and see what it returns…
> 
> shai

Shai, I cant believe i missed that! Thank you very much for responding.

Nate

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [June 13, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/acts-as-tree-problem-accessing-parent-object/63205/4 "2006-06-13T17:13:56Z")

</div>

Anytime 😉

Shai
