We’ve gotten a report request in, and I’m trying to find out the easiest and most efficient way to pull the numbers.
I have a Deal model with two attributes: _quantity_purchased_ and price. I’ve been asked to retrieve a list of the top 100 best selling deals in the database. To see how well a deal has done, we multiply the quantity_purchased by the price.
Now, I’m sure I could write something convoluted to get my results, but this seems like such a simple operation, that there has got to be an easier way of doing it. Is there something I could do in MySQL, or using Ruby/Rails? Or am I stuck finding some kind of unpleasant loop?
FYI, we are running Rails 2.3.
You can pass a
:limitparameter to yourfindmethod to limit the number of results returned. Combined with a:orderparameter, you can get the ‘top’ 100 results:Note as of Rails 3, the correct way of writing this query would be