Fetch a Rails ActiveRecord 'datetime' attribute as a DateTime object.
- by Tobias Cohen
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?