# How to get to the parent of a child object

**URL:** <https://www.ruby-forum.com/t/how-to-get-to-the-parent-of-a-child-object/98271>\
**Category:** Rails\
**Created:** [July 13, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/how-to-get-to-the-parent-of-a-child-object/98271 "2006-07-13T17:09:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [July 13, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/how-to-get-to-the-parent-of-a-child-object/98271/1 "2006-07-13T17:09:38Z")

</div>

Hi, i have a problem with the following:

I have an application with patients \<–\>\>consults  
models are ok

in the view list\_consults i do this:

for consult in @consults%\>

  
\<%= consult.patient.name %\>  
\<%= consult.reason %\>  
\<%= consult.date %\>  
\<%= link\_to ‘Show’, :action =\> ‘show\_consult’, :id =\> consult  
%\>  
  
\<% end %\>

in my controler i have:

def list\_consults  
@consults\_pages, @consults = paginate :consults, :per\_page =\> 8  
end

i cant get to the lastname of the patient, the error i get

"undefined method `name’ for nil:NilClass

what am i doing wrong?

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [July 13, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/how-to-get-to-the-parent-of-a-child-object/98271/2 "2006-07-13T17:28:57Z")

</div>

I don’t know if this is the case or not, but if you are not requiring a  
patient to be assigned elsewhere then some of the records will not have  
a patient record. When it loops and reaches one such consult, then the  
error will be thrown. You can fix it by giving the line a condition:

\<%= consult.patient.name if consult.patient %\>

Hope this helps…

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [July 13, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/how-to-get-to-the-parent-of-a-child-object/98271/3 "2006-07-13T17:56:58Z")

</div>

Me again. There has got to be a better way of doing what I just wrote  
there. With what I have there will be an additional query for each  
consult record to see if it has a patient. If you have this in a lot of  
rows and columns on a list page, then that’s a lot of queries. It does  
the job, but with a lot of extra querying.

I’m not sure of another way of doing that…

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [July 13, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/how-to-get-to-the-parent-of-a-child-object/98271/4 "2006-07-13T19:04:15Z")

</div>

robert bradford wrote:

> I don’t know if this is the case or not, but if you are not requiring a  
> patient to be assigned elsewhere then some of the records will not have  
> a patient record. When it loops and reaches one such consult, then the  
> error will be thrown. You can fix it by giving the line a condition:
> 
> \<%= consult.patient.name if consult.patient %\>
> 
> Hope this helps…

thanks a lot robert,  
indeed there was a problem in my table, there were some records in it  
with no parent. now it works

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [July 13, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/how-to-get-to-the-parent-of-a-child-object/98271/5 "2006-07-13T19:00:59Z")

</div>

robert bradford wrote:

> \<%= consult.patient.name if consult.patient %\>

> There has got to be a better way of doing what I just wrote  
> there. With what I have there will be an additional query for each  
> consult record to see if it has a patient.

I disliked that by-product as well and instead have been using:

\<%= consult.patient.name if consult.patient\_id %\>

---

<div class="post-metadata">

**Author:** ![remcoh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@remcoh](https://www.ruby-forum.com/u/remcoh)\
**Post date:** [July 13, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/how-to-get-to-the-parent-of-a-child-object/98271/6 "2006-07-13T19:41:42Z")

</div>

Lonnie Warpup wrote:

> robert bradford wrote:
> 
> > \<%= consult.patient.name if consult.patient %\>
> 
> > There has got to be a better way of doing what I just wrote  
> > there. With what I have there will be an additional query for each  
> > consult record to see if it has a patient.
> 
> I disliked that by-product as well and instead have been using:
> 
> \<%= consult.patient.name if consult.patient\_id %\>

Hi Lonnie-

I just tried changing one of my lists to do this, but I’m still showing  
individual queries being done. In my application I have help desk  
tickets with the following belongs\_tos:

department  
user  
category  
assignee

On my list I want to show each of these. In my tickets controller I have  
the following:

@ticket\_pages, @tickets = paginate :tickets, :per\_page =\> 10,  
:conditions =\> conditions, :include =\> [:department, :category, :user,  
:assignee]

The :include option should (and does) create joins in the tickets query.  
I assume this would then cause those associations to not be loaded  
separately. In my view I have the following:

\<% for ticket in @tickets %\>

 #... \<%=h ticket.user.full\_name if ticket.user\_id %\> \<%=h ticket.department.name if ticket.department\_id %\> \<%=h ticket.category.name if ticket.ticket\_category\_id %\> \<%=h ticket.assignee.full\_name if ticket.assignee\_id %\> #... \<% end %\>

When load the page my terminal window shows individual queries being  
done still. Am I missing something?

Thanks,  
Robert
