Reputation: 2610
I have two time fields in the table i.e. start_time and end_time
.
When I execute MyModel.save(start_time: '12:34')
, it gets saved with appending a date(Sat, 01 Jan 2000 07:25:00 UTC +00:00)
.
I want to save time only. I am using Rails5
Upvotes: 6
Views: 4414
Reputation: 2610
When we execute, a = MyModel.save(start_time: '12:34')
, it saves only time in the database. But when we display the value in the console(a.start_time
), we gets time with date. So we have to retrieve time from it by the following:
a.start_time.strftime("%H:%M")
Data gets correctly saved in the database
Upvotes: 2
Reputation: 312
Rails will always append date with time even its type is "time". You can achieve storing only time using sql query. But i would suggest you to use one of the following options:
1)store time in column of type fixnum or decimal in 24hrs format like "22.44"
2) Store number of seconds in column of type integer & then write a helper method to confirm it into time.
3)Use column type :time and ignore the date whole querying.
Upvotes: 0