Fetch a Rails ActiveRecord 'datetime' attribute as a DateTime object.

Posted by Tobias Cohen on Stack Overflow See other posts from Stack Overflow or by Tobias Cohen
Published on 2010-05-17T08:49:31Z Indexed on 2010/05/17 9:10 UTC
Read the original article Hit count: 168

I have an attribute in one of my models that contains a Date/Time value, and is declared using t.datetime :ended_on in my migrations.

When I fetch this value using myevent.ended_on, I get a Time object. The problem is that when I try to use this attribute as an axis in a Flotilla chart, it doesn't work properly because Flotilla only recognizes dates as Date or DateTime objects.

I thought about writing a virtual attribute that will convert the existing Time value to a DateTime, but I'm wary of doing this, since I've heard that Time can't handle dates later than 2040, and I don't wish to risk creating a "2040 bug" to worry about later.

Is there any way I can persuade ActiveRecord to return DateTime objects for this attribute instead of Time objects?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord