# Validates\_presence\_of Date with '00' year fails

**URL:** <https://www.ruby-forum.com/t/validates-presence-of-date-with-00-year-fails/135740>\
**Category:** Rails\
**Created:** [April 24, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/validates-presence-of-date-with-00-year-fails/135740 "2008-04-24T16:00:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olly\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/eb8c5e/32.png) [@Olly\_L](https://www.ruby-forum.com/u/Olly_L)\
**Post date:** [April 24, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/validates-presence-of-date-with-00-year-fails/135740/1 "2008-04-24T16:00:10Z")

</div>

Hi there

I’ve come across an interesting scenario with validates\_presence\_of  
and Dates in Rails. I’ve been looking through the Rails code but I  
just can’t figure out where in Rails the date parsing which presumably  
causes this issue is happening.

The problem is this. If you try and create a model using a date  
string in the format “dd MMM yy” but you specify a year of ‘00’,  
‘validates\_presence\_of’ fails because the Date object Rails converts  
the String to is nil. If you specify any other 2-digit year it’s  
fine. Here’s a simple example test case:

class Model  
validates\_presence\_of :dated\_on  
end

# this works

m = Model.create(:dated\_on =\> ‘20 Apr 01’)  
assert m.errors.empty?

# this fails

m = Model.create(:dated\_on =\> ‘20 Apr 00’)  
assert m.errors.empty?

Has anyone come across this before and/or would anyone be able to  
point me to where in the Rails code base the date is parsed so I can  
look at implementing a workaround? I just can’t spot it!

Many thanks  
Olly

---

<div class="post-metadata">

**Author:** ![Olly\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/eb8c5e/32.png) [@Olly\_L](https://www.ruby-forum.com/u/Olly_L)\
**Post date:** [April 25, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/validates-presence-of-date-with-00-year-fails/135740/2 "2008-04-25T16:55:10Z")

</div>

Ultimately, the parsing of a date comes down to the new\_date method in  
activerecord/lib/active\_record/connection\_adapters/abstract/schema\_definitions.rb.  
It’s currently on line 166 of that file in edge Rails. That method  
returns  
nil if the year is 0. I don’t know why it works that way, but at least  
you  
now know why you see the behavior that you do.

Regards,  
Craig
