Digitalism Digitalism - 1 month ago 9
Ruby Question

if statement in ruby loop editing properties

I have two database tables, user and product
When the user creates a product the user.id is placed into the product table under product.user_id

I'm writing a loop to displays all products "belonging" to the currently signed in user, it looks like this so far.

<% @product.each do |p| %>
<% if product.user_id = user.id %>
<tr>
<td><%= user.id %></td>
<td><%= product.user_id %></td>
<td><%= product.id %></td>
<td><%= product.name %></td>
</tr>
<% end %>
<% end %>


If the currently signed in user is user 2 for example, instead of filtering all products that don't have a user.id of 2 it just changes the product.user_id for every product to 2 so that the returned table looks like so.

2 2 1
2 2 2
2 2 3
2 2 4


Without the if statement it returns the full product listing with all the correct properties.

Answer

You should use == not =:

<% if product.user_id == user.id %>
Comments