set application time zone dynamically (rails)
I need to set config.time_zone
according to a record in my database. Inside of rails initialization block, I don't have access to the models because they haven't been loaded yet, I think.
I tried to run the query without the model, with the code below, but the connection is not es开发者_StackOverflowtablished yet at this moment.
require 'active_record'
timezone = ActiveRecord::Base.connection.execute
Creating a before filter and set Time.zone every request doesn't help me.
Is there a way around this?
updated:
figured it out putting this at the end of environment.rb:
timezone = SiteConfig.find_by_attribute("timezone").attribute_value
zone_default = Time.__send__(:get_zone, timezone)
Time.zone_default = zone_default
ActiveRecord::Base.time_zone_aware_attributes = true
ActiveRecord::Base.default_timezone = :utc
the code was extracted from rails initialization file.
精彩评论