简体   繁体   中英

Rails 7 - PG "timestamp with time zone" attributes are Time instead of ActiveSupport::TimeWithZone

In upgrading to Rails 7 from Rails 6, followed by bundle exec rails app:update , suddenly model attributes wrapping Postgres timestamp with time zone columns are returning as Time objects instead of ActiveSupport::TimeWithZone objects.

Structure.sql example

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

before (Rails 6)

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

after (Rails 7)

Blog.last.created_at.class #=> Time

How can I get Rails to recognize that I want these columns to be wrapped with ActiveSupport::TimeWithZone ?

This has been fixed but not yet released .

To fix in the short term you'll need to explicitly tell Rails to parse this type of column with a time zone.

In an initializer, add :timestamptz as a time_zone_aware_types value.

# config/initializers/active_record.rb

ActiveRecord::Base.time_zone_aware_types << :timestamptz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM