How to get to the parent of a child object

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?

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…

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…

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

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 %>

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs