Skip to content

Commit

Permalink
change to use products (not variants) as the unit of best-seller coun…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
paulcc committed Jul 2, 2009
1 parent c001eef commit 7e86c7d
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions app/models/promotions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ def self.best_sellers(n, start = 1.week.ago, finish = Time.now)
best_n = Order.checkout_completed(true).
between(start,finish).
find(:all,
:joins => "INNER JOIN line_items ON orders.id = line_items.order_id",
:select => "variant_id, SUM(quantity) sum",
:group => "variant_id ORDER BY sum DESC",
:joins => "INNER JOIN line_items ON orders.id = line_items.order_id" +
" INNER JOIN variants ON variant_id = variants.id",
:select => "product_id, SUM(quantity) sum",
:group => "product_id ORDER BY sum DESC",
:limit => n)

best_n.map {|o| [o.sum, Variant.find(o.variant_id).product] }
best_n.map {|o| [o.sum, Product.find(o.product_id)] }
end

end

0 comments on commit 7e86c7d

Please sign in to comment.