Checking date of birth is greater than today

hi,

I need to check 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

You probably want Date.today or Date.today.to_s depending on what dob is.

If I do this:

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

I get this error:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.>

If I do this:

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

I get this error:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.>

Well if dob is nil then you know what your problem is :slight_smile:

Fred

Sorry, I forgot to enter date from dob drop down menus. I still get
this error:
undefined method `/' for Tue Nov 20 15:38:43 -0500 2007:Time

And this prints correct date:
puts "dob: " + read_attribute(:dob).to_date.to_s

This is the code:

  def validate_on_create()
    puts "dob: " + read_attribute(:dob).to_date.to_s
    if read_attribute(: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

Sorry, I forgot to enter date from dob drop down menus. I still get
this error:
undefined method `/' for Tue Nov 20 15:38:43 -0500 2007:Time

This is the code:

def validate_on_create()
   puts "dob: " + read_attribute(:dob).to_date.to_s
   if read_attribute(: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

Have you checked the documentation for the Date class? If you do
you'll see that Date#new isn't expecting an instance of Time
I'd go with Greg's suggestion of Date::today

Fred

I am getting this error: undefined method `-' for "2007-11-20":String

def validate_on_create()
    puts "dob: " + read_attribute(:dob).to_date.to_s
    if read_attribute(:dob) > Date.new(Date.today.to_s)
      errors.add("dob", "Date of birth must be less than " +
Time.now.to_formatted_s(:my_format_0) + ".")
    end
  end

I am getting this error: undefined method `-' for "2007-11-20":String

def validate_on_create()
   puts "dob: " + read_attribute(:dob).to_date.to_s
   if read_attribute(:dob) > Date.new(Date.today.to_s)
     errors.add("dob", "Date of birth must be less than " +
Time.now.to_formatted_s(:my_format_0) + ".")
   end
end

Date.new doesn't take a string either. Why are you complicating this
beyond dob > Date.today ?

Fred

I already tried that and I got:

comparison of Date with Time failed

Why is dob a Time and not a date?

I already tried that and I got:

comparison of Date with Time failed

dob = Date.new(2007,11,21)
=> #<Date: 4908851/2,0,2299161>
>> dob > Date.today
=> true

I already tried that and I got:

comparison of Date with Time failed

This is so circular. Why not do dob > Time.now then ? (or
Time.now.midnight)

Fred

Assuming dob is a Date, and not a Time, then you could write it this way:

def validate_on_create
      errors.add("dob", "Date of birth must not be in the future.") if dob > Date.today
  end

if dob is a Time but you're not actually concerned about the time of day, then this will work:

def validate_on_create
      errors.add("dob", "Date of birth must not be in the future.") if dob.to_date > Date.today
end

I found out that as long as I enter DOB(from new form), I get same
error no matter what. Even if I do this:

dob1 = Date.new(2007,11,21)
    dob2 = Date.new(2006,11,21)
    if dob1 > dob2

This is the line that error message indicates and I don't understand
why this always gives error no matter what I do in that def.

Showing app/views/attending_ips/_form.rhtml where line #14 raised:
..l.
...
<TD valign=top><FONT COLOR="151b8d" FACE="arial,geneva,helvetica"
SIZE="-1"><B>DOB</B><FONT SIZE="-2"> (mm/dd/yyyy)<BR><%= date_select
'attending_ip', 'dob', :order =>
[:month, :day, :year], :use_month_numbers => true, :include_blank =>
true, :start_year => 1970, :end_year => Time.now.year %>

Have I done something wrong in this view?

As I mentioned like below, I get same error. The only difference in
that error message is that if I use your first way, error message
comes from model, If I try second, it comes from view.

If you're getting an error it must be that dob is not what you think it is.
Can you post your view and controller code?

def create

    @attending_ip = AttendingIp.new(params[:attending_ip])
    @attending_ip.last_name = @attending_ip.last_name.upcase
    @attending_ip.first_name = @attending_ip.first_name.upcase
    if @attending_ip.save
      flash[:notice] = 'Patient record was successfully created.'
      redirect_to :action => 'search'
    else
      render :action => 'new'
    end
  end

I found the error. Actually, it was coming from other def and I didn't
realized that. Thanks for all your help!

A simpler option could be just have this in your view template which
will only display dates from today onwards
<%= date_select(:person, "dob", :start_year =>
Time.now.strftime("%Y").to_i, :end_year => 1950) %>