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?
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.>
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
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
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
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.
…
…
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
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?
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) %>
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.