Skip to content

Instantly share code, notes, and snippets.

@phaedryx
Created September 23, 2012 03:32
Show Gist options
  • Save phaedryx/3768744 to your computer and use it in GitHub Desktop.
Save phaedryx/3768744 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
has_many :items
...
def self.ranked(year)
select("rank() OVER(ORDER BY SUM(items.amount) DESC), users.*, SUM(items.amount) AS grand_total")
.joins(:items)
.where("EXTRACT(YEAR FROM items.sold_at)=:year", year: year)
.group("users.id")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment