Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Checking date of birth is greater than today

Posted on 2007-11-20
4
Medium Priority
?
837 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20326311
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
ID: 20375605
@dkim18:

Do you need further assistance with comparing dates?

Thanks,
Phil / peh803
0
 
LVL 1

Accepted Solution

by:
rbarazi earned 1400 total points
ID: 20423438
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 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