I have this scope on my product model:
scope :all_products, lambda {
joins(:prices)
.where('prices.start_date <= ? and products.available = ?', Time.current, true)
.uniq
.order('CASE WHEN products.quantity >= products.min_quantity and (prices.finish_date IS NULL OR prices.finish_date >= now()) THEN 0 ELSE 1 END, prices.finish_date asc')
}
I get the follow error when I try to run it: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
How I can use my order by and the query be uniq? I use rails 4.