Skip to content

Instantly share code, notes, and snippets.

@JonathonMA
Forked from justinhennessy/gist:6173402
Last active December 20, 2015 20:29
Show Gist options
  • Save JonathonMA/6190572 to your computer and use it in GitHub Desktop.
Save JonathonMA/6190572 to your computer and use it in GitHub Desktop.
class Challenge
def user_with_highest stat = :distance
User.with_highest stat, period, users
end
def period
start_date..end_date
end
end
class User
def self.with_highest stat, period = (10.years.ago)..(10.years.from_now), users = all
users.max { |a, b| a.activities.total_stat_between(period, stat) > b.activities.total_stat_between(period, stat) }
end
end
class Activity
def self.total_stat_between(period, stat)
between(period).sum(stat)
end
def self.between(period)
where(date: period)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment