mysql - Most efficient way to retrieve "top x" records using Rails -


we've gotten report request in, , i'm trying find out easiest , efficient way pull numbers.

i have deal model 2 attributes: _quantity_purchased_ , price. i've been asked retrieve list of top 100 best selling deals in database. see how deal has done, multiply quantity_purchased price.

now, i'm sure write convoluted results, seems such simple operation, there has got easier way of doing it. there in mysql, or using ruby/rails? or stuck finding kind of unpleasant loop?

fyi, running rails 2.3.

you can pass :limit parameter find method limit number of results returned. combined :order parameter, can 'top' 100 results:

deal.find(:all, :order => 'quantity_purchased * price', :limit => 100); 

note of rails 3, correct way of writing query be

deal.order('quantity_purchased * price').limit(100) 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -