【问题标题】:Rails 7 - PG "timestamp with time zone" attributes are Time instead of ActiveSupport::TimeWithZoneRails 7 - PG \"timestamp with time zone\" 属性是 Time 而不是 ActiveSupport::TimeWithZone
【发布时间】:2022-08-12 04:09:27
【问题描述】:

从 Rails 6 升级到 Rails 7,然后是 bundle exec rails app:update,突然包装 Postgres timestamp with time zone 列的模型属性返回为 Time 对象而不是 ActiveSupport::TimeWithZone 对象。

Structure.sql 示例

CREATE TABLE public.blogs (
    id bigint NOT NULL,
    created_at timestamp with time zone,
    #...

之前(轨道 6)

Blog.last.created_at.class #=> ActiveSupport::TimeWithZone

之后(轨道 7)

Blog.last.created_at.class #=> Time

如何让 Rails 识别我希望这些列用ActiveSupport::TimeWithZone 包裹?

    标签: ruby-on-rails postgresql activerecord


    【解决方案1】:

    这有been fixednot yet released

    要在短期内修复,您需要明确告诉 Rails 使用时区解析这种类型的列。

    在初始化程序中,添加 :timestamptz 作为 time_zone_aware_types 值。

    # config/initializers/active_record.rb
    
    ActiveRecord::Base.time_zone_aware_types << :timestamptz
    

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 2023-01-10
      • 2011-03-14
      • 2020-09-20
      • 1970-01-01
      • 2021-08-24
      • 2016-07-09
      • 1970-01-01
      • 2012-01-07
      相关资源
      最近更新 更多