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%> <tr> <td><%= consult.patient.name %></td> <td><%= consult.reason %></td> <td><%= consult.date %></td> <td><%= link_to ''Show'', :action => ''show_consult'', :id => consult %></td> </tr> <% 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? -- Posted via http://www.ruby-forum.com/.
robert bradford
2006-Jul-13 15:30 UTC
[Rails] Re: how to get to the parent of a child object.
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... -- Posted via http://www.ruby-forum.com/.
robert bradford
2006-Jul-13 15:57 UTC
[Rails] Re: how to get to the parent of a child object.
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... -- Posted via http://www.ruby-forum.com/.
Lonnie Warpup
2006-Jul-13 17:01 UTC
[Rails] Re: how to get to the parent of a child object.
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 %> -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
robert bradford
2006-Jul-13 19:32 UTC
[Rails] Re: how to get to the parent of a child object.
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 %> <tr class="<%= cycle ''odd'', ''even'' %>"> #... <td><%=h ticket.user.full_name if ticket.user_id %></td> <td><%=h ticket.department.name if ticket.department_id %></td> <td><%=h ticket.category.name if ticket.ticket_category_id %></td> <td><%=h ticket.assignee.full_name if ticket.assignee_id %></td> #... </tr> <% end %> When load the page my terminal window shows individual queries being done still. Am I missing something? Thanks, Robert -- Posted via http://www.ruby-forum.com/.