【问题标题】:Best way to save only time in rails with postgres database?使用 postgres 数据库仅在 Rails 中节省时间的最佳方法?
【发布时间】:2021-02-07 21:33:33
【问题描述】:

我有一个小小的想法,在 rails 中只存储时间的最佳方法是什么?

我脑子里可能有 3 个选项

  1. 将其存储为 DateTime 但忽略日期部分
  2. 将其存储为以秒/分钟为单位的整数
  3. 将其存储为小数

您可以向我建议什么,为什么? :)

Postgres 13

Rails 6.0.3

Ruby 2.7.2

【问题讨论】:

  • 您可以使用time 值。

标签: ruby-on-rails ruby postgresql


【解决方案1】:

time is a built-in Rails column typeTime objects 一起使用。它将使用Postgres time (without time zone) type

您可以手动使用time with time zone,它也会映射到时间,但会保留时区。

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

有关如何在 Rails 中处理时间的更多信息,请参阅 It's About Time (Zones)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多