13

I am working on a Rails project using a Postgres database. For one of my models, I have a time column, called (cleverly) time. When I created the model, I set the data type for this column as 'time', with the (perhaps incorrect) understanding that this data type was for storing time only, no date.

t.time :time

However, when I submit data to the model, the time is correct but prefixed by an incorrect date:

time: "2000-01-01 16:57:19"

I just want the column to store the time (like '16:57:19'). Is 'time' the correct data type to use? Or is there some other way I should handle this problem?

Thank you very much.

3
  • 2
    Rails should map the time type to the Postgres time type. Are you sure you are using postgres and not sqlite for the development environment? Commented Jan 24, 2016 at 18:07
  • @max: The OP isn't mapping anything, ActiveRecord is. See below. Commented Jan 24, 2016 at 18:48
  • Yes - I set it up to use Postgres in development Commented Jan 24, 2016 at 20:07

1 Answer 1

26

The problem is that there is no time-of-day class in Ruby or Rails. All the time classes are dates or timestamps (i.e. date plus time of day).

Inside the database it will be a time (without timezone) column and it will behave properly inside the database. However, once the time gets into Ruby, ActiveRecord will add a date component because there is no plain time-of-day class available, it just happens to use 2000-01-01 as the date.

Everything will be fine inside the database but you'll have to exercise a little bit of caution to ignore the date component when you're outside the database in Rails.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok that's interesting, thank you. I didn't know that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.