Skip to content

Instantly share code, notes, and snippets.

@verdi327
Created February 19, 2013 22:46
Show Gist options
  • Save verdi327/4990905 to your computer and use it in GitHub Desktop.
Save verdi327/4990905 to your computer and use it in GitHub Desktop.
#InvoiceItem Class
def self.filter_by_item(item_id)
InvoiceItem.all_successful.select { |ii| ii.item_id == item_id }
end
def self.revenue_per_item(item_id)
filter_by_item(item_id).collect { |ii| ii.unit_price * ii.quantity }.inject(:+)
end
#Item Class
def most_revenue(total_num)
hash = Hash.new
InvoiceItem.all_successful.each do |ii|
hash[ii.item_id] = InvoiceItem.revenue_per_item(ii.item_id)
end
sorted_revenue = hash.each { |k,v| v.sort }.reverse
sorted_revenue[0..total_num]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment