I need to get a list of '10 most popular' products. I think that the best way to do this is to use Orders
table (model) that has a reference to Products
.
I know what I should do but I don't know how to write it. This is how I would do this in a MySQL:
SELECT products.*
FROM (SELECT product_id, count(product_id) as count from Orders
GROUP BY product_id
ORDER BY count DESC
LIMIT 10) AS O
LEFT OUTER JOIN products
ON Products.id = O.product_id
How I can write the query in Rails?
For example:
Order.group(:product_id).count...