lundi 29 décembre 2014

Ruby sentence with dates and calibrations

I have Items and Calibrations table. The items can have multiples calibrations. The table calibrations contains (date_calibration, date_expired, item_id). I must show the items with calibrations, and if it is has more than one, show only the max. Then I need write an IF sentence and show a new column with color (red, yellow and green) depending on how many days left to calibrate the item. using date_expired.


Item controller (show the max, works good)



@items = Item.joins(:calibrations)
.order('items.id')
.order('calibrations.date_expired DESC')
.select('DISTINCT ON (items.id) items.id, items.codigo, items.numero, items.den_cont, genre_id, state_id, affectation_id, sector_id, person_id, calibrations.date_expired AS Fecha_Vencimiento')


Index.html



<% @items.each do |item| %>
<tr>
<td><%= item.codigo + '%03d' % item.numero.to_s %></td>
<td><%= item.den_cont %></td>
<td><%= item.genre.genre %></td>
<td><%= item.state.state %></td>
<td><%= item.affectation.affectation %></td>
<td><%= item.sector.sector %></td>
<td><%= item.calibrations.pluck(:date_expired).max %></td>

<td><%= if item.calibrations(:date_expired) <= Date.today + 60 then image_tag '/semaforo/amarillo_small.png'
elsif item.calibrations(:date_expired) <= Date.today + 30 then image_tag '/semaforo/rojo_small.png'
else image_tag '/semaforo/verde_small.png' end %></td>


I have an error in the sentence if "compared with non class/module". Can you help me with the correct sentence.? If date_expired is less than today + 60 days, must show a yellow image...then less than today + 30 days red image and the others green image..


Aucun commentaire:

Enregistrer un commentaire