views:

324

answers:

1

For some reason I can't save a Time to MySQL. My Column is a datetime and I've also tried using a timestamp when I was testing. Here's my console session below:

$ ./script/console
Loading development environment (Rails 2.1.0)
>> Event.find 1
# Event id: 1, name: "Meeting at XXX", start_date: "2009-01-17"
>> e.start_date = Time.now
=> Thu Jan 22 16:40:01 0000 2009
>> e.save
=> true
>> e.start_date
=> Thu Jan 22 16:40:01 0000 2009
>> e.reload
# Event id: 1, name: "Meeting at XXX", start_date: "2009-01-22"

The time part is just getting ignored in updates and querys.

A: 

Turns out it was caused by this old code from from Rails 1 or lower days.

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  :date_month => "%d %b",
  :dd_mm_yyyy => "%d/%m/%y",
  :yyyy_mm_dd  => "%Y-%m-%d"
)

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
    ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS
)

Removing that solved the problem.