[英]How to change default timezone for Active Record in Rails?
在我的application.rb
中,我遇到了以下評論
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Eastern Time (US & Canada)'
正如您從上面看到的,我已將config.time_zone
設置為 EST 時間。 但是,仍然在數據庫中創建記錄時, datetime
時間似乎是以 UTC 格式存儲的。
在上面的評論中,他們說
...並使 Active Record 自動轉換到該區域...
我該怎么做,在哪里?
另外,我也將在 heroku 上部署它,我希望該設置能夠延續
將以下內容添加到application.rb
作品
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local # Or :utc
我決定編譯這個答案,因為所有其他答案似乎都不完整。
config.active_record.default_timezone確定在從數據庫中提取日期和時間時是使用 Time.local(如果設置為:local)還是 Time.utc(如果設置為:utc)。 默認值為:UTC。 http://guides.rubyonrails.org/configuring.html
如果您想更改Rails時區,但繼續讓Active Record以UTC格式保存在數據庫中,請使用
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
如果您想更改Rails時區並在該時區有Active Record存儲時間,請使用
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local
警告:在以非 UTC 格式在數據庫中保存時間之前,您真的應該三思而后行。
筆記
修改application.rb
后不要忘記重新啟動 Rails 服務器。
請記住config.active_record.default_timezone
只能取兩個值
config.time_zone
中定義的時區)以下是查找所有可用時區的方法
rake time:zones:all
在痛苦萬分之后,我得出了與院長佩里相同的結論。 config.time_zone = 'Adelaide'
和config.active_record.default_timezone =:local
是成功的組合。 這是我在此過程中發現的。
就我而言(Rails 5),我最終在我的app/config/environments/development.rb
中添加了這兩行
config.time_zone = "Melbourne"
config.active_record.default_timezone = :local
就是這樣,為了確保正確讀取墨爾本:我在終端中運行了命令:
bundle exec rake time:zones:all
墨爾本在我所在的時區上市!
如果要將時區設置為全局 UTC,可以在 Rails 4 中執行以下操作:
# Inside config/application.rb
config.time_zone = "UTC"
config.active_record.default_timezone = :utc
請務必重新啟動您的應用程序,否則您將看不到更改。
在軌道 4.2.2、go 到application.rb
並使用config.time_zone='city'
(例如:'London' 或 'Bucharest' 或 'Amsterdam' 等)。
它應該工作得很好。 它對我有用。
我不得不將此塊添加到我的environment.rb
文件中,一切都很好:)
Rails.application.configure do
config.time_zone = "Pacific Time (US & Canada)"
config.active_record.default_timezone = :local
end
Rails.application.initialize!
行之前添加了它! 對於中國用戶,只需在config/application.rb
中添加以下兩行:
config.active_record.default_timezone = :local
config.time_zone = 'Beijing'
在 Ruby on Rails 6.0.1 go 中配置>語言環境> application.rb y agrega lo siguiente:
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module CrudRubyOnRails6
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
config.active_record.default_timezone = :local
config.time_zone = 'Lima'
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
end
end
您可以看到我正在用 2 行配置時區:
config.active_record.default_timezone =: local
config.time_zone = 'Lima'
我希望它可以幫助那些在 Rails 6.0.1 上使用 Ruby 的人
如果要設置本地時間,請在application.rb
中添加以下文本
config.time_zone = 'Chennai'
# WARNING: This changes the way times are stored in the database (not recommended)
config.active_record.default_timezone = :local
然后重啟你的服務器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.