# Rails 2.1.1 setting datetime to nil at 00:00:00

**URL:** <https://www.ruby-forum.com/t/rails-2-1-1-setting-datetime-to-nil-at-0000/152437>\
**Category:** Rails\
**Created:** [November 3, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/rails-2-1-1-setting-datetime-to-nil-at-0000/152437 "2008-11-03T23:03:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [November 3, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/rails-2-1-1-setting-datetime-to-nil-at-0000/152437/1 "2008-11-03T23:03:29Z")

</div>

Hello,  
There appears to be a strange bug in Rails 2.1.1 where if I set a  
datetime  
field to YYYY-MM-DD 00:00:00 (using TimeZone support) Rails returns nil,  
instead of YYYY-MM-DD 00:00:00

Here is confirmed output:

> > c = Clip.find(53385)  
> > =\> #\<Clip id: 53385, title: “test 2”, description: “test2”, keywords:  
> > “test2”, publish\_date: “2008-11-03 11:13:00”\>
> 
> > c.publish\_date = “2008-11-03 19:00:00”  
> > =\> “2008-11-03 19:00:00”
> 
> > c.save  
> > =\> true
> 
> > c = Clip.find(53385)  
> > =\> #\<Clip id: 53385, title: “test 2”, description: “test2”, keywords:  
> > “test2”, publish\_date: nil\>
> 
> > c.publish\_date  
> > =\> nil

Any reason for this behavior ?

Adam

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [November 4, 2008, 12:17am UTC](https://www.ruby-forum.com/t/rails-2-1-1-setting-datetime-to-nil-at-0000/152437/2 "2008-11-04T00:17:16Z")

</div>

> =\> #\<Clip id: 53385, title: “test 2”, description: “test2”, keywords:  
> “test2”, publish\_date: “2008-11-03 11:13:00”\>
> 
> > > c.publish\_date = “2008-11-03 19:00:00”  
> > > =\> “2008-11-03 19:00:00”
> > 
> > > c.save  
> > > =\> true
> > 
> > > c = Clip.find(53385)  
> > > =\> #\<Clip id: 53385, title: “test 2”, description: “test2”, keywords:  
> > > “test2”, publish\_date: nil\>
> > 
> > > c.publish\_date  
> > > =\> nil

Your example isn’t setting it to 00:00:00, but 19:00:00 ?

Also, what does the database say? I’d want to see it confirmed there  
as well. And lastly, are there any before/after filters on this  
model? Any triggers in the database that might affect it?

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [November 4, 2008, 1:08am UTC](https://www.ruby-forum.com/t/rails-2-1-1-setting-datetime-to-nil-at-0000/152437/3 "2008-11-04T01:08:09Z")

</div>

I used 19:00 b/c of Time zones (EST is GMT-5). I have a before\_filter  
that  
sets the time zone to either the users time zone, or EST by default.  
Database is storing it as 00:00:00 b/c we store as UTC  
before\_filter: Time.zone = current\_user.timezone ?  
current\_user.timezone : $DEFAULT\_TIME\_ZONE where DEFAULT is EST

mysql\> select id,publish\_date from clips where id=53385 ;  
±------±--------------------+  
| id | publish\_date |  
±------±--------------------+  
| 53385 | 2008-11-04 00:00:00 |  
±------±--------------------+

Its a datetime field in DB  
| publish\_date | datetime

Adam

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [November 4, 2008, 2:50am UTC](https://www.ruby-forum.com/t/rails-2-1-1-setting-datetime-to-nil-at-0000/152437/4 "2008-11-04T02:50:27Z")

</div>

confirmed to be an issue with validates\_date\_time  
[http://www.semergence.com/2006/12/20/validates\_date\_time-not-ready-for-prime-time/](http://www.semergence.com/2006/12/20/validates_date_time-not-ready-for-prime-time/)

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [November 4, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/rails-2-1-1-setting-datetime-to-nil-at-0000/152437/5 "2008-11-04T15:44:23Z")

</div>

Whenever I need to store and manipulate a date independent of time, I  
prefer using the SQL Date type and corresponding ActiveSupport Date  
type. It’s a lot cleaner for parsing and validating.
