Simm Simm - 3 months ago 10
Ruby Question

if statement using created_at

<% if product.created_at %>
<span class="label label-warning">New!</span>
<% end%>


Web dev newbie.. Can't figure out how to use if statement in rails view to display the span class when product records is up to 3 days old. Any ideas?

I can query in my console with

variable = Product.where(created_at: 3.days.ago..Time.now)


and get the the correct records. But cannot understand how to translate this into rails.

The ERB block is going into a _partial

dkp dkp
Answer

You can try this:

<% if product.created_at > 3.days.ago.beginning_of_day %>
  <span class="label label-warning">New!</span>
<% end%>