I have a complicated web application that has a multi record set on the top half of the page and single record details on the bottom half of the page when I select a record and also as part of the lower details portion I have partials that I update with AJAX that are multi record tables from model relationships, but the problem I’m having is when I try doing a loop through a record set and try to get data through the relationships of that record, the record comes back nil. Here’s an example of what I mean.
<table>
<% @file.file_entities.each do |i| %>
<tr>
<td id="name"><div><a href="#"><%= truncate("#{i.entity.FirstName} #{i.entity.LastName}", :length => 17) %></a></div></td>
<td id="position"><select><option>Buyer</option><option>Seller</option><option>Lender</option><option>Referrer</option></select></td>
<td id="pr"><%= check_box_tag('pr_out', i.PRSent, i.PRSent?) %></td>
<% if EntityContact.where(:EntityID => i.entity.EntityID).phones_callable.first.inspect == nil -%>
<td id="phone"><div>NONE</div></td>
<% else %>
<td id="phone"><div><%= EntityContact.where(:EntityID => i.EntityID).phones_callable.first.ContactDesc %>: <%= EntityContact.where(:EntityID => i.EntityID).phones_ca>
<% end %>
</tr>
<% end %>
</table>
Where I’m using this to get the value in ContactDesc
EntityContact.where(:EntityID => i.EntityID).phones_callable.first.ContactDesc
It’s giving me a nil EntityContact model object, but if I put just the i.EntityID in this loop then it gives me the EntityID and also if I replace i.EntityID with an actual ID in my where clause above then it will return that record just fine. So I think i.EntityID isn’t working in my where clause, but I don’t know why?
I figured it out, stupid me didn’t realize that if there wasn’t a record in that relationship, it would throw an exception, so I just added an if statement checking if there existed a record first before pulling data, my bad.