Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用实例变量控制器在视图中进行if查询_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 使用实例变量控制器在视图中进行if查询

Ruby on rails 使用实例变量控制器在视图中进行if查询,ruby-on-rails,ruby,Ruby On Rails,Ruby,我又卡住了 我的/视图中有此代码/ <% if DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","closed"]).last %> <% @Specimen_ids.each do |ds| %> <th><%= ds.id %></th> <% end %> </tr> <tr> <

我又卡住了

我的/视图中有此代码/

<% if DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","closed"]).last %>
  <% @Specimen_ids.each do |ds| %>
 <th><%= ds.id %></th>
   <% end %>
 </tr>
 <tr>
  <% @all_results.each do |result| %>
   <td><%= result.distribution_sheet_id %></td><td><%= result.lab_id %></td><td><%= result.kit %></td><td><%= result.batch_number %></td><td><%= result.cutoff %></td>
  <% @Specimen_results.each do |sr| %>
  <td><%= sr.result_user %></td>
  <% end %>
  </tr>
   <% end %>
  <%- end -%>
  </tr>
我想说的是,仅当最后一张分发表是“已关闭”的,然后仅为该已关闭的分发表输出上述详细数据

上面的代码确实输出了数据,但是对于所有的分发表。如果第一个国家没有采取任何行动。我希望我已经解释过了。如果你想要更多的解释,那么请告诉我。 非常感谢。

您需要:

if DistributionSheet.find(:all).last.lifecycle_stated == "closed"
  #code
end
它显示了所有的DisubtionSheets,因为您正在抓取所有“关闭”的记录,并检查该数组中的最后一个元素是否为nil/false

您需要上面的代码,因为您正在获取所有图纸,然后检查最后一个重新绘制的图纸是否实际上已关闭,正如所述,这是所需的结果

两个旁注:

1) 您真的不应该在视图中有DB调用

2) 你真的应该限制你的结果,而不是:全部。如果数据库中有大量记录,这将返回一个巨大的数据集

if DistributionSheet.find(:all).last.lifecycle_stated == "closed"
  #code
end