Bad DateTime Support in Ruby………..but wait!
It’s been a few days since I last worked with Rails, but this one issue that came up has been bugging me. The ActiveRecord support in Rails is pretty cool, and I like that they have different db provider (even Sql Server!). But, I recently inserted the following code into one of my models:
before_create : on_before_create
def on_before_create
self.CreatedDate = DateTime.now
self.LastModified = DateTime.now
end
For those of you coming from .net, this is analogous to adding an event handler (on_before_create) to an event (before_create) in my model. Then I go on to define the on_before_create method where I set the created date and last modified date of my model to the current date/time. I thought I was being clever by being able to remove these two fields from my view, because users shouldn’t have to modify these fields anyways, so I figured I could remove some of the hassle. But, when I go to insert this item into the database, the INSERT statement has a syntax error in the date value, because the dates come out like this:
2006-09-27T19:55:22-0500
which MySql doesn’t like. After doing some searching on DateTime’s in Ruby, I gave up until just now.
As I was writing this post I planned to lament the poor DateTime support in Ruby, but literally as I wrote the last paragraph I had one more idea about how to make it work, so I tried it out and it worked perfectly, heres the difference:
before_create : on_before_create
def on_before_create
self.CreatedDate = DateTime.now.to_s
self.LastModified = DateTime.now.to_s
end
All I did was convert the DateTime objects into their string representation and it worked, no problem! So I guess my bitch-fest will have to wait for another obstacle ![]()
Jake Good said,
Wrote on September 27, 2006 @ 9:18 pm
Were you using datetime or timestamp?
I’m pretty sure I’ve never had to do this… And another thing, if you name your columns right, Ruby will do this for you!
ben reichelt said,
Wrote on September 27, 2006 @ 10:55 pm
Jake, I’m using datetime fields, you’re saying that if I name my columns correctly, rails will know which field to update on INSERTS (CreatedDate and LastModified) and which fields to update on UPDATES (only LastModified, not CreatedDate) ??
Ryan said,
Wrote on September 28, 2006 @ 6:21 am
Hi Ben,
Rails will automagically update fields named updated_on or updated_at on UPDATE and created_on created_at on INSERT. I’m not sure but they may need to be TimeStamp rather than DateTime (if indeed there is any difference)
There’s a list of fields here.. http://wiki.rubyonrails.org/rails/pages/MagicFieldNames
ben reichelt said,
Wrote on September 28, 2006 @ 8:24 am
Ryan, thanks for the link thats sweet, I’ll have to go change my columns names now
ben said,
Wrote on September 30, 2006 @ 10:34 am
this is just a test
Sam said,
Wrote on October 15, 2006 @ 5:29 pm
The short of it is that you’ll get the behaviour you expect if you use the “Time” class instead.
Sam said,
Wrote on October 16, 2006 @ 8:48 am
Hey Ben, that probably sounded a little short. Not what I intended, it just got my gears turning on how to best address this issue and I wanted to get the short answer out there.
If you’re curious, you’ve inspired this post: http://substantiality.net/articles/2006/10/16/date-time-and-datetime-in-ruby where I explain in detail the behaviour you’re seeing. I did my best, but any criticisms are welcome. 