Solved

Checking date of birth is greater than today

Posted on 2007-11-20
4
828 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
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 350 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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