Solved

Checking date of birth is greater than today

Posted on 2007-11-20
4
823 Views
Last Modified: 2008-02-01
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
Comment
Question by:dkim18
4 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
 
LVL 19

Expert Comment

by:peh803
Comment Utility
@dkim18:

Do you need further assistance with comparing dates?

Thanks,
Phil / peh803
0
 
LVL 1

Accepted Solution

by:
rbarazi earned 350 total points
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now