Skip to content

Conversation

andyatkinson
Copy link
Member

@andyatkinson andyatkinson commented Nov 20, 2024

This addition to the Active Record PostgreSQL Rails Guide documents how to use a configuration option to change the default timestamp data type from timestamp without time zone to timestamp with time zone (timezone-aware timestamps).

Support for this data type was added to Active Record earlier but is not used by default, requiring a configuration change to use. From the PR comments where it was added, there was a suggestion to document how to use this configuration option, prompting this PR.

I created a new Rails 8 application, generating migrations for scaffolds, and realized they weren't using this data type. I made this configuration change, destroyed and regenerated my scaffold migration, and verified the new data type was used.

I welcome your feedback on this guide content, placement within the guide, or anything else.

Thanks!

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@exterm
Copy link

exterm commented Feb 13, 2025

would be great to merge this, eh?

@ghiculescu ghiculescu added the ready PRs ready to merge label Feb 13, 2025
This guide addition documents how to configure your Rails application to
use the data type "timestamp with time zone" (timezone-aware timestamp),
instead of the default type, "timestamp without time zone"

Support for this data type was added earlier in Active Record, but
requires a configuration change in order to use, and from these PR
comments, where, why, and how to perform this configuration change
wasn't added to the Rails Guides Active Record PostgreSQL documentation
from what I could tell

Related PR: rails#41084

Guide page: https://guides.rubyonrails.org/active_record_postgresql.html

PostgreSQL best practices:
https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_timestamp_.28without_time_zone.29
@Edouard-chin Edouard-chin force-pushed the docs/rails-guides-postgresql-timestamp-with-time-zone branch from 1332c6f to f65d592 Compare March 24, 2025 12:41
@Edouard-chin Edouard-chin merged commit 9f39c01 into rails:main Mar 24, 2025
3 checks passed
Edouard-chin added a commit that referenced this pull request Mar 24, 2025
…sql-timestamp-with-time-zone

Rails Guides: PostgreSQL timestamp with time zone [ci skip]
Edouard-chin added a commit that referenced this pull request Mar 24, 2025
…sql-timestamp-with-time-zone

Rails Guides: PostgreSQL timestamp with time zone [ci skip]
@exterm
Copy link

exterm commented Mar 24, 2025

❤️

# config/application.rb
ActiveSupport.on_load(:active_record_postgresqladapter) do
self.datetime_type = :timestamptz
end
Copy link
Member

@skipkayhil skipkayhil Mar 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be inside config.after_initialize?

Why is that necessary? This block will run whenever the load hook fires, so the location of this code shouldn't matter.

Edit: original comment was deleted(?) so I've added the quote as context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs ready PRs ready to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants