Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Condition in each do Rails

Hi i need to print out just the candidates where active is == 0 here is my code in the view.

I can print if active is yes or no.. But in the each do loop i just want to print the active candidates.

So how can i add the condition to my each do loop, thanks.
  <% @candidates.each do |candidate| %>
    <div id="candidateper">
  <div class="avatth" ><div class="avat_min">
    <% if candidate.avatar.present? %>
       <%= link_to (image_tag candidate.avatar.url(:thumb)), (candidate_path(candidate))  %>
     <% else %>
     <%= link_to (image_tag ("espanol/playersample.png")), (candidate_path(candidate))  %>

     <% end %>
  </div></div>



      <div class="nameth"><%= candidate.name %></div>
      <div class="activeth"><%= candidate.active ? t('generales.yess') : t('generales.noo') %></div>
      <div class="generalth">
        <% if candidate.user.purchased_at.present? %>
          <%= candidate.user.purchase_defeated? ? t('generales.defeated') : t('generales.active') %>
        <% else %>
          <%=  t('generales.noo') %>
        <% end %>
      </div>

      <div class="actionsth"><%= link_to t('generales.show'), candidate_path(candidate) %>
      <% if current_user.user_type == 'admin' %>
      <%= link_to t('generales.delete'), candidate_path(candidate), method: :delete, data: { confirm: t('generales.delete_candidate_confirm') } %>
     <% end %>
    </div>
    </div>
  <% end %>

</div>
  <% end %>

I`ve tried this

no luck syntax error on all my ideas :P

like image 554
Moncho Chavez Avatar asked Sep 23 '13 01:09

Moncho Chavez


1 Answers

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 %>
like image 185
mu is too short Avatar answered Sep 24 '22 00:09

mu is too short