About ActiveRecord default_timezone

Assumed version

Ruby 2.5 series Ruby on Rails 5.2 series

Explanation

In Rails, the timezone of the time stamp issued by ActiveRecord is defined in config/application.rb.

# config/application.rb 

Rails.application.configure do
  config.active_record.default_timezone = :local
  # (ActiveRecord::Base.default_timezone = :Synonymous with local)
Omission
end

What this: local refers to is the ruby ​​process timezone. The ruby ​​process timezone looks at the environment variable TZ defined in the OS. In other words, if you don't set this environment variable, ActiveRecord will issue a timestamp with the default UTC.

For example, if you define as follows, created_at and updated_at will be the time in Tokyo.

TZ=Asia/Tokyo

Difference from config.time_zone

Speaking of time zones set in Rails, I think that there are also the following people.

Rails.application.configure do
  config.time_zone = 'Tokyo'
end

This is a setting that allows you to set the time zone acquired by Time.zone to Tokyo.

You can get the current time in Tokyo by hitting Time.zone.now on the application.

reference

Rails Time Zone Summary

Recommended Posts

About ActiveRecord default_timezone
About ActiveRecord :: PendingMigrationError
About =
About method.invoke
About Kotlin
About Hinemos
About inheritance
About params
About Docker
About Rails 6
About form_for
About enum
About polymorphism
About Optional
About hashes
About JitPack
About Dockerfile
About this ()
About devise
About encapsulation
About Docker
About JAVA_HOME
About active_hash
About static
About exceptions
About scope
[Maven] About Maven
[Rails] ActiveRecord
[Rails / ActiveRecord] About the difference between create and create!