Created
January 12, 2012 17:19
-
-
Save rthbound/1601845 to your computer and use it in GitHub Desktop.
Hot products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#To load in-stock parts ordered within last 30 or 7 days: | |
#For PartNumbers I have: | |
named_scope :with_id, lambda {|id| { :conditions => { :id => id } } } | |
named_scope :stocked, :conditions => ['quantity > ?', 0] | |
#For Orders I have: | |
named_scope :within_days, lambda { |number| {:conditions => ["created_at > ? AND transaction_id IS NOT NULL", Time.now - number.days] } } | |
PartNumber.with_id(Order.within_days(7).map{|o| o.order_products.map{|op| op.part_number_id}}.flatten.uniq).stocked.count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PartNumber.ordered_between(7.days.ago, Time.zone.now)
class PartNumber
named_scope :ordered_between, lambda{|start_time, end_time| { :conditions => ["orders.created_at BETWEEN ? AND ?", start_time, end_time], :include => [:order] }
end