Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 839
  • Last Modified:

Checking date of birth is greater than today

hi,

I need to dob is greater than today and this is what I did, but I get this error:

undefined method `/' for Tue Nov 20 14:21:26 -0500 2007:Time

++++++++++
class AttendingIp < ActiveRecord::Base
...
...
def validate_on_create(today = Date::today)
    if dob >  Date.new(Time.now)
      errors.add("dob", "Date of birth must be less than " + Time.now.to_formatted_s(:my_format_0) + ".")
    end
  end
++++++++++

thx,
0
dkim18
Asked:
dkim18
1 Solution
 
Geert BormansCommented:
Time.now generates a time string in this format "Wed Nov 21 09:50:01 +0100 2007"
you can't use that in calculations,
so this will not work
Date.new(Time.now)

if you need to create a Date object from today,
all you need is
today = Date.today
0
 
peh803Commented:
@dkim18:

Do you need further assistance with comparing dates?

Thanks,
Phil / peh803
0
 
rbaraziCommented:
Use this snippet if your dob column is date (which is how it's supposed to be), otherwise use Time.now instead
validate_on_create :validate_dob
 
private
  def validate_dob
    if self.dob >  Date.today
      errors.add("dob", "Date of birth must be less than #{Date.today}")
    end
  end

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now