Topic: simpler date comparisons

I've written a custom validation to stop people setting dates in the past for this particular model (the column is :deadline):

  def validate
    compare_time = self.deadline.to_a[3..5] <=> Time.now.to_a[3..5]
    errors.add(:deadline,"can't be set in the past") if compare_time < 0
  end

This makes sure dates are not in the past without bothering with seconds, minutes, etc which don't matter. It does the job but is there a DRYer way of doing it?

Thanks

Chris

Last edited by cmowforth (2008-05-29 09:54:08)

Re: simpler date comparisons

def validate
errors.add(:deadline,"can't be set in the past") if self.deadline < Time.today
end