简体   繁体   中英

Rails is ignoring the time part of my datetime column

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.

I had a similar issue, the date part of a column was not being saved. This was because the type was time and not datetime .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM