0

我有一个复杂的 Web 应用程序,当我选择一条记录时,它在页面的上半部分设置了多条记录,在页面的下半部分设置了单条记录详细信息,并且作为下部详细信息部分的一部分,我有部分要更新使用 AJAX 是来自模型关系的多记录表,但是我遇到的问题是,当我尝试通过记录集进行循环并尝试通过该记录的关系获取数据时,记录又回来了nil。这是我的意思的一个例子。

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

我在哪里使用它来获取价值ContactDesc

EntityContact.where(:EntityID => i.EntityID).phones_callable.first.ContactDesc

它给了我一个 nilEntityContact模型对象,但是如果我只把i.EntityID它放在这个循环中,那么它给了我,EntityID并且如果我在上面的 where 子句中替换i.EntityID为一个实际ID的,那么它会很好地返回那个记录。所以我认为i.EntityID在我的 where 子句中不起作用,但我不知道为什么?

4

1 回答 1

0

我想通了,愚蠢的我没有意识到如果这种关系中没有记录,它会抛出异常,所以我只是添加了一个 if 语句,在提取数据之前先检查是否存在记录,我的错。

于 2012-07-18T22:18:47.407 回答