Rails time fields return
I have a simple Rails 3.b1 (Ruby 1.9.1) application running on Sqlite3. I have this table:
create_table :time_tests do |t|
t.time :time
end
And I see this behavior:
irb(main):001:0> tt = TimeTest.new
=> #<TimeTest id: nil, time: nil>
irb(main):002:0> tt.time = Time.zone.now
=> Mon, 03 May 2010 20:13:21 UTC +00:00
irb(main):003:0> tt.save
=> true
irb(main):004:0> TimeTest.find(:first)
=> #<TimeTest id: 1, time: "2000-01-01 20:13:21">
So time comes back. When checking the table, the data looks fine:
sqlite> select * from time_tests;
1|2010-05-03 20:13:21.774741
I'm guessing this is on the search part? What's going on here?
Technically this is not refundable. It is returned as a time with a default date. It returns 2000-01-01 20:13:21
as the time expected.
Rails does some magical loading of data into the Time object and clears up the date (since you just told it to keep the time).
If you want to store date and time, you need to define the column as datetime
. Conversely, if you only want a date, you should use date
.
So, to recap:
date => "2010-12-12 00:00:00"
time => "2000-01-01 13:14:15"
datetime => "2010-12-12 13:14:15"
a source to share