# Dates in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/dates-in-activerecord/147162>\
**Category:** Rails\
**Created:** [August 28, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/dates-in-activerecord/147162 "2008-08-28T22:58:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2acd7d/32.png) [@Abel](https://www.ruby-forum.com/u/Abel)\
**Post date:** [August 28, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/dates-in-activerecord/147162/1 "2008-08-28T22:58:17Z")

</div>

I’m going nuts over this incidence in my code: I have this class  
AdsSources \< ActiveRecord::Base and one of it’s fields is a date, but  
when I update this field and read it again, I always get the same  
wrong date like this:

@a = AdsSources.find :first  
@a.last\_time\_polled = Time.now;  
@a.save!

I don’t get any Exceptions from this operation and it doesn’t matter  
if I use a different method like update\_attributes, the result will be  
the same. Now, and this can be as soon as the following line in my  
code, I read this value like this:

@a.last\_time\_polled

And no matter when or how I updated this field the last time, I always  
get the same date:

Sat Jan 01 22:43:26 +0100 2000

January 1st 2000. Could someone point me a reason for this? Thanks.

---

<div class="post-metadata">

**Author:** ![Abel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2acd7d/32.png) [@Abel](https://www.ruby-forum.com/u/Abel)\
**Post date:** [August 28, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/dates-in-activerecord/147162/2 "2008-08-28T23:00:03Z")

</div>

On 28 Aug 2008, at 21:57, Abel wrote:

> And no matter when or how I updated this field the last time, I always  
> get the same date:
> 
> Sat Jan 01 22:43:26 +0100 2000

> January 1st 2000. Could someone point me a reason for this? Thanks.

What’s in the database ?

Fred

---

<div class="post-metadata">

**Author:** ![Abel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2acd7d/32.png) [@Abel](https://www.ruby-forum.com/u/Abel)\
**Post date:** [August 29, 2008, 4:29am UTC](https://www.ruby-forum.com/t/dates-in-activerecord/147162/3 "2008-08-29T04:29:28Z")

</div>

Use a :datetime field type instead of plain :date. I’m supposing thats  
the data type you have in your migration.  
Or if the date is not important, you can go with a pure :time  
approach.

---

<div class="post-metadata">

**Author:** ![Abel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2acd7d/32.png) [@Abel](https://www.ruby-forum.com/u/Abel)\
**Post date:** [August 29, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/dates-in-activerecord/147162/4 "2008-08-29T13:51:57Z")

</div>

Thanks for your replies, guys.  
I checked the content in the database and found that only the time of  
the day was being stored, so I changed the type of the column to  
‘timestamp’ and it’s working properly now.
