I am using Rails 3.0.3 where I have quite an extensive validation process.
I want to make a validation where I check if a certain variable is a proper date
Basically this is what I want to do is:
def validate_date(this_date)?
begin
Date.parse(this_date)
rescue
proper_date = false
else
proper_date = true
end
end
and call this method in my model using:
attr_accessor :given_date
validate_date(given_date)
but also at another place in my validation file using for example:
attr_accessor :another_date
validate_date(another_date)
or something similar…
What do I need to do to make this work?
What you should do is something similar to nkm’s answer:
Or you can create validator for your date and use it as you use standard validators.
Refer to documentation on how to create custom validator if you prefer keep your models cleaner.