Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.