简体   繁体   中英

Rails 3.2 Can't get data to display from model relations

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?

我弄清楚了,愚蠢的我没有意识到如果在那个关系中没有记录,它将抛出异常,所以我只是添加了一个if语句,在提取数据之前首先检查是否存在记录,这很糟糕。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM