레일에서 활성 레코드의 기본 표준 시간대를 변경하는 방법은 무엇입니까?
내 안에서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동부 표준시까지그러나 DB에 레코드가 생성되면 다음과 같이 나타납니다.datetimeUTC 형식으로 저장 중입니다.
위의 댓글에서 그들은 말합니다.
활성 레코드를 이 영역으로 자동 변환합니다.
제가 어떻게, 그리고 어디서 그것을 할 수 있습니까?
또한, 나는 이것을 헤로쿠에도 배포할 것이고 나는 설정을 넘겨주기를 원합니다.
다른 것들이 모두 불완전한 것 같아 이 답변을 정리하기로 했습니다.
config.active_record.default_timezone은 데이터베이스에서 날짜 및 시간을 가져올 때 Time.local(:local로 설정된 경우) 또는 Time.utc(:utc로 설정된 경우)를 사용할지 여부를 결정합니다.기본값은 :utc입니다.http://guides.rubyonrails.org/configuring.html
Rails 표준 시간대를 변경하지만 활성 레코드가 UTC의 데이터베이스에 저장되도록 하려면 다음을 사용합니다.
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
레일 표준시를 변경하고 이 표준시에 활성 레코드 저장 시간을 설정하려면 다음을 사용합니다.
# application.rb
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local
경고: UTC가 아닌 형식으로 데이터베이스에 시간을 저장하기 전에 두 번 또는 세 번 생각해야 합니다.
메모
수정 후 Rails 서버를 다시 시작하는 것을 잊지 마십시오.application.rb.
그 것을 기억하라.config.active_record.default_timezone두 개의 값만 사용할 수 있습니다.
- :local(에 정의된 시간대를 기준으로 함)
config.time_zone) - :utc(UTC로 변환)
사용 가능한 모든 표준 시간대를 찾을 수 있는 방법은 다음과 같습니다.
rake time:zones:all
다음에 추가application.rb작동하다
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = :local # Or :utc
저는 많은 고민 끝에 딘 페리와 같은 결론에 도달했습니다. config.time_zone = 'Adelaide'그리고.config.active_record.default_timezone = :local승리의 조합이었습니다.제가 그 과정에서 발견한 것은 다음과 같습니다.
저의 경우(레일 5), 저는 결국 이 두 줄을 추가했습니다.app/config/environments/development.rb
config.time_zone = "Melbourne"
config.active_record.default_timezone = :local
바로 그거야!멜버른이 올바르게 읽혔는지 확인하기 위해 터미널에서 명령을 실행했습니다.
bundle exec rake time:zones:all
멜버른은 제가 있는 시간대에 등록되어 있었어요!
표준 시간대를 전체적으로 UTC로 설정하려면 레일 4에서 다음을 수행할 수 있습니다.
# Inside config/application.rb
config.time_zone = "UTC"
config.active_record.default_timezone = :utc
응용 프로그램을 다시 시작해야 변경 내용이 표시됩니다.
나는 이 블록을 내 블록에 추가해야 했습니다.environment.rb파일과 모든 것이 잘 있었습니다 :)
Rails.application.configure do
config.time_zone = "Pacific Time (US & Canada)"
config.active_record.default_timezone = :local
end
- 선 앞에 추가했습니다.
Rails.application.initialize!
Ruby on Rails 6.0.1에서 config > locales > application.rb로 이동하고 다음을 추가합니다.
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
두 줄로 시간대를 구성하는 것을 볼 수 있습니다.
config.active_record.default_timezone =: local
config.time_zone = 'Lima'
Ruby와 Rails 6.0.1에서 작업하는 사람들에게 도움이 되었으면 합니다.
레일 4.2.2에서 다음으로 이동합니다.application.rb 및사를 합니다.config.time_zone='city'(예: '런던' 또는 '부쿠레슈티' 또는 '암스테르담' 등).
잘 될 겁니다.그것은 나에게 효과가 있었다.
사용자의 두하면 됩니다.config/application.rb:
config.active_record.default_timezone = :local
config.time_zone = 'Beijing'
를 현시간을설다텍추가다니합를스트에 추가합니다.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
그런 다음 서버를 다시 시작합니다.
언급URL : https://stackoverflow.com/questions/6118779/how-to-change-default-timezone-for-active-record-in-rails
'programing' 카테고리의 다른 글
| 값이 없는 테이블에 삽입하기 위한 구문? (0) | 2023.06.25 |
|---|---|
| 어떻게 하면 과거의 약속을 쉽게 고칠 수 있습니까? (0) | 2023.06.25 |
| 팬더를 사용하여 python에서 Excel 파일 읽기 (0) | 2023.06.25 |
| MS SQL Server의 형식 번호(백분율) (0) | 2023.06.25 |
| 팬더 시리즈를 데이터 프레임으로 변환 (0) | 2023.06.25 |