If Condition in each do Rails

南笙酒味 提交于 2019-12-05 18:20:16

If candidate.active is actually a boolean then you could say:

<% @candidates.reject(&:active).each do |candidate| %>
    ...
<% end %>

If @candidates is actually an ActiveRecord::Relation then you could probably say:

<% @candidates.where(:active => false).each do |candidate| %>
    ...
<% end %>

to avoid pulling a bunch of stuff out of the database when you don't want it.

If active is actually a number (inside the database and outside the database) then you could say:

<% @candidates.select(&:zero?).each do |candidate| %>
    ...
<% end %>

or

<% @candidates.where(:active => 0).each do |candidate| %>
    ...
<% end %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!