# Validations question

**URL:** <https://www.ruby-forum.com/t/validations-question/184613>\
**Category:** Rails\
**Created:** [March 5, 2010, 7:08am UTC](https://www.ruby-forum.com/t/validations-question/184613 "2010-03-05T07:08:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [March 5, 2010, 7:08am UTC](https://www.ruby-forum.com/t/validations-question/184613/1 "2010-03-05T07:08:41Z")

</div>

I have wrote an validation function for checking that my starting date  
should be greater than the current date as like

validate :start\_date\_should\_not\_be\_greater\_than\_current\_date

def start\_date\_should\_not\_be\_greater\_than\_current\_date  
if self.start\_date \> Date.today  
errors.add(“Whatever”)  
end  
end

## But the problem is that when i left empty the field for start\_date when filling values in form, this error is coming:-

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

I have also wrote some exceptions for my methods but these exceptions  
are not catching this error.

Can you guys tell me how to tackle with this situation…

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [March 5, 2010, 9:46am UTC](https://www.ruby-forum.com/t/validations-question/184613/2 "2010-03-05T09:46:17Z")

</div>

If the field is empty, then self.start\_date is nil. Try this:

def start\_date\_should\_not\_be\_greater\_than\_current\_date  
if !self.start\_date.nil?  
if self.start\_date \> Date.today  
errors.add(“Whatever”)  
end  
end  
end

or something similar

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [March 5, 2010, 10:01am UTC](https://www.ruby-forum.com/t/validations-question/184613/3 "2010-03-05T10:01:31Z")

</div>

> validate :start\_date\_should\_not\_be\_greater\_than\_current\_date

Personally I’d rename that to:

valid :start\_date\_before\_now

And then I’d do this as the method:

def start\_date\_before\_now  
if start\_date && start\_date \> Date.today  
errors.add(“Whatever”)  
end  
end

Just check start\_date first. You don’t need the .nil? check as nil  
evaluates to false (and any valid date is true). Note, you don’t need  
to do  
self.start\_date when reading a value, only when assigning to it (the  
reason  
is that reading the value will default to calling the method if no local  
variable exists, but writing a value will default to creating a local  
variable rather than calling the value= method).

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![hemantborole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b782af/32.png) [@hemantborole](https://www.ruby-forum.com/u/hemantborole)\
**Post date:** [March 5, 2010, 11:51am UTC](https://www.ruby-forum.com/t/validations-question/184613/4 "2010-03-05T11:51:05Z")

</div>

Andy J. wrote:

> > validate :start\_date\_should\_not\_be\_greater\_than\_current\_date
> 
> Personally I’d rename that to:
> 
> valid :start\_date\_before\_now
> 
> And then I’d do this as the method:
> 
> def start\_date\_before\_now  
> if start\_date && start\_date \> Date.today  
> errors.add(“Whatever”)  
> end  
> end

Thanks andy… This worked like a charm … 🙂

> Cheers,
> 
> Andy
