28
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails5のtimezone設定は普通にapplication.rbでよかった

Posted at

これまでアプリケーションで共通の設定はconfig/application.rbに記述していたが、Rails5からは

config/application.rb
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

とあるように、config/initializers/以下に個別にファイル作ってそこで設定しようという方針になっているようだ。

そこでconfig/initializers/time_zone.rbというファイルを新規作成し

Rails.application.config.time_zone = 'Tokyo'

としたが、Time.zoneはUTCのままでViewでレンダリングされるcreated_atも変わらず。。

ググったところ以下の情報が見つかった。

But you shouldn't assign some values such as time_zone in an initializer. Because Rails.application.config.time_zone will work, but Time.zone won't. Time.zone_default is assgined before the initializer.

どうやらinitializers/*.rbが走る前にTime.zone_defaultが初期化されているらしい。該当のソースコードはこちら

ということで結局Rails5以前と同じようにconfig/application.rbconfig.time_zone = 'Tokyo'を追記することでタイムゾーン設定を変更できた。

28
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?