Viktor
Viktor

Reputation: 353

Best way to save only time in rails with postgres database?

I'm having a small mindblow, what's the best way to store only time in rails?

Likely I'm having 3 options in my head

  1. Store it as DateTime but ignore the date part
  2. Store it as an integer in seconds/minutes
  3. Store it as a decimal

What you could suggest to me and why? :)

Postgres 13

Rails 6.0.3

Ruby 2.7.2

Upvotes: 1

Views: 1241

Answers (1)

Schwern
Schwern

Reputation: 165546

time is a built-in Rails column type which works with Time objects. It will use the Postgres time (without time zone) type.

You can manually use time with time zone which will also map to Time, but it will retain the time zone.

add_column(:yourtable, :time_at, 'time with time zone')

See It's About Time (Zones) for more about how to work with time in Rails.

Upvotes: 1

Related Questions