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

++++++++++
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
++++++++++
any ideas?

thx,

On Nov 20, 2007 1:50 PM, kimda [email protected] wrote:

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

def validate_on_create(today = Date::today)
if dob > Date.new(Time.now)

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


Greg D.
http://destiney.com/

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.>

On 20 Nov 2007, at 20:11, kimda wrote:

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

On Nov 20, 3:19 pm, Frederick C. [email protected]

On 20 Nov 2007, at 20:43, kimda wrote:

 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

On 20 Nov 2007, at 20:59, kimda wrote:

Date.new doesn’t take a string either. Why are you complicating this
beyond dob > 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

On Nov 20, 3:50 pm, Frederick C. [email protected]

I already tried that and I got:

comparison of Date with Time failed

On Nov 20, 4:05 pm, Frederick C. [email protected]

On Nov 20, 2007, at 3:13 PM, kimda wrote:

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

On Nov 20, 2007 3:13 PM, kimda [email protected] wrote:

I already tried that and I got:

comparison of Date with Time failed

Why is dob a Time and not a date?


Greg D.
http://destiney.com/

On 20 Nov 2007, at 21:13, kimda wrote:

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

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.

DOB (mm/dd/yyyy)
<%= 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?

On Nov 20, 4:21 pm, Frederick C. [email protected]

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.

On Nov 20, 2007, at 1:50 PM, kimda wrote:



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
++++++++++
any ideas?

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

On Nov 20, 2007, at 4:08 PM, kimda wrote:

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?

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

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

++++++++++++

new.rhtml

Create new inpatient Sign-Out Database Inpatient Information

<% form_tag :action => ‘create’ do %>
<%= render :partial => ‘form’ %>
<%= submit_tag “Create” %>



** PRINT
not available until a patient is created **

<%= render :partial => “attending_ips/menu” %>
<% end %>

+++++++++++++++

_form.rhtml


DOB (mm/dd/yyyy)
<%= date_select 'attending_ip', 'dob', :order => [:month, :day, :year], :use_month_numbers => true, :include_blank => true, :start_year => 1970, :end_year => Time.now.year %> ... ...

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) %>