Tim Tim - 6 months ago 17
Ruby Question

Postgres table column looping

I have a

Movie Review
table with
approve
column thats a
boolean
. Would I be able loop through each reviews and see if any of the reviews are
true
?

If any(at least one) the reviews are approved (
true
), then display it, else display
<p>No reviews at the moment...</p>


So far I have this..I know this only check if any review are in the database and not if they are
true
or
false
. Not sure how to get it right.

<% if !@movie.reviews.present? %>
<p>No reviews at the moment. Be the first!</p>
<% else %>
<% @movie.reviews.each do |review| %>
<%= render "/reviews/review" , review: review%>
<% end %>
<% end %>

Answer

try this..

<% unless @movie.reviews.where("reviews.approve = ?", true).present? %>
   <p>No reviews at the moment. Be the first!</p>
<% else %>
  <% @movie.reviews.each do |review| %>
    <% if review.approve %>
      <%= render "/reviews/review" , review: review%>
    <% end %>
  <% end %>
<% end %>