How would I override <=> for certain attributes of a class?
Say I have the following:
class Workout < ActiveRecord::Base
# columns in db: time, weight, reps, distance
I want to be able to sort by results, to list workouts by best time,
weight, etc. But the ordering is different, because a low time is
better than a high time, while a high weight is better than a low
Right now I'm doing a class method with an if statement like this:
if compare == :time
That works, but it's ugly and the ugliness tends to replicate itself.
I could simplify my code by overriding the <=> method for the time
attribute so that I could simply do:
But how to do that?