ruby-on-rails - rails :How to create a time column with timezone on postgres

标签 ruby-on-rails postgresql timezone utc

我有一个在 postgres 数据库上运行的 rails 应用程序。我有一个名为 UserTimings 的模型,其中有两个字段 from_time 和 to_time。

t.time     "from_time"
t.time     "to_time"

我预计时间将以带有时区信息的完整 UTC 格式存储。过了一会儿,我意识到数据库有这种创建表的 SQL 查询。

from_time time without time zone,
to_time time without time zone,

我不想要这个。我想存储时区。我想要 UTC 时间的 +0530 东西,虽然 Rails 应用程序已配置为处理它,但我没有得到。请告诉我如何编写迁移以强制数据库使用时区。

谢谢。

最佳答案

迁移完全按照您的意愿进行

class CreatePeppers < ActiveRecord::Migration
  def change
    create_table :peppers do |t|
      t.column :runs, 'timestamp with time zone'
      t.column :stops, 'time with time zone'
    end
  end
end

类型供您选择。你可以在这里写任何 postgresql 支持的类型。但是转换为类型可能会出现问题,rails 可以理解。

关于ruby-on-rails - rails :How to create a time column with timezone on postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19207856/

相关文章:

ruby-on-rails - 从 Controller 请求对象中获取 Authorization header 的正确方法是什么?

ruby-on-rails - rails 上的 ruby : Is it possible to nest partial calls/rendering like so:

ruby - 如何将时间字符串解析为特定时区?

django - Postgres 和 Django - 数据错误 : time zone not recognized

ruby-on-rails - 如果你使用belongs_to 没有相应的has_one 会发生什么?

ruby-on-rails - 如何使用 paypal_permissions gem 获取 PayPal 的用户数据和交易详细信息?

postgresql - 从 DO 语句中的查询中获取结果

案例中的 SQL 顺序、DESC 和 ASC 奇怪行为

postgresql - 用于验证电子邮件地址的 Postgres 函数

Android TimeZone 土耳其 GMT