A user enters two dates. On before_save, I’m trying to validate that
the second date does not come before the first.
Assume that start_date and end_date are Datetime objects. If I call
the following method from the model on before_save, why is no error
added to end_date?
def assure_dates_in_order
start = Time.at(self.start_date)
last = Time.at(self.end_date)
if (start - last) > 0
errors.add(:base, “can’t come before start date”)
end
end
I know know that this code is quite ugly. I could not get the
Datetime objects to compare any other way.
Anyone who can make this code beautiful and functional is awesome.