导轨 6.1.4.1
我正在从初始化文件设置值,当我请求页面时,我发现object_id
配置更改(并且值丢失),除非我require 'my_library'
在初始化文件中这样做。我不明白为什么?
应用程序/lib/feature_flags.rb:
class FeatureFlags
class Configuration
include ActiveSupport::Configurable
config_accessor(:my_key) { false }
end
class << self
def configuration
@configuration ||= Configuration.new
end
def configure
yield configuration
end
def enabled?(feature)
puts "#{__FILE__} FeatureFlags.configuration.object_id = #{FeatureFlags.configuration.object_id}"
configuration[feature]
end
end
end
配置/初始化程序/feature_flags.rb:
# require 'feature_flag' # If I uncomment this line, the problem is solved
puts "#{__FILE__} FeatureFlags.configuration.object_id = #{FeatureFlags.configuration.object_id}"
FeatureFlags.configure do |config|
config.my_key = true
end
输出:
1. Run the rails server:
config/initializers/feature_flags.rb FeatureFlags.configuration.object_id = 14720
2. Request some page:
app/lib/feature_flags.rb FeatureFlags.configuration.object_id = 22880
我的问题是:
- 为什么我需要
require 'feature_flags'
在初始化器中使 object_id 不改变?我认为 Zeitwerk 正在处理这个问题。 - 这就是我应该做的(做正确的事)吗?
谢谢你的帮助!