How do I write a "distinct on" finder method in Rails?

Viewed 3473

I'm using RoR 5.0.1 with PostGres 9.5. I have the following query that gets distinct records from a specific table (represented by a corresponding model) based on a date

select distinct on (crypto_currency_id) t.* 
from crypto_prices t 
order by crypto_currency_id, last_updated desc;

My model name is CryptoPrice, but I'm unclear how to convert the above into a finder method. This

CryptoPrice.distinct.pluck(:crypto_currency_id)

doesn't do the same thing, as I discovered.

1 Answers
Related