Skip to content

Instantly share code, notes, and snippets.

@rthbound
Created January 12, 2012 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rthbound/1601845 to your computer and use it in GitHub Desktop.
Save rthbound/1601845 to your computer and use it in GitHub Desktop.
Hot products
#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
@knewter
Copy link

knewter commented Jan 12, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment