I am wandering if you can help me with this.
I have this query on SQL and I need to do it in my rails app.
These query's represent the same result and works good.
SELECT DISTINCT ON (i.id)
i.id, i.den_cont, c.date_expired
FROM Items i
JOIN Calibrations c ON c.item_id = i.id
ORDER BY i.id, date_expired desc
and
select codigo, numero, den_cont, date_expired
from items
join calibrations c
on items.id = c.item_id
inner join (select item_id, MAX(date_expired) as FECHA
from calibrations
group by item_id) as i2
on c.item_id = i2.item_id and c.date_expired = i2.FECHA
I have this, I need to change it to do the above..
@items = Item.joins(:calibrations).where('calibrations.date_expired <= ?', '2014/09/01')