如何使用域对象从中间件cache_classes了吗?
-
13-09-2019 - |
题
在Rails开发环境,cache_classes
是关闭的,所以你可以在app/
修改代码,看看变化,无需重新启动服务器。
在所有环境中,虽然,中间件只创建一次。所以,如果我有中间件这样的:
class MyMiddleware
def initialize(app)
@app = app
end
def call(env)
env['model'] = MyModel.first
end
end
和我为此在config/environments/development.rb
:
config.cache_classes = false # the default for development
config.middleware.use MyMiddleware
然后我将永远得到以下错误:
A copy of MyMiddleware has been removed from the module tree but is still active!
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:414:in `load_missing_constant'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:96:in `const_missing'
/Users/me/projects/my_project/lib/my_middleware.rb:8:in `call'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/middleware_stack.rb:72:in `new'
...
在的问题是,MyMiddleware
实例在系统负载时创建一次,但MyModel
类被重新装载在每个呼叫。
我试图'MyModel'.constantize.first
延迟结合到类直到方法呼叫时间,但是这改变了问题到一个新的:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.include?
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/active_record/attribute_methods.rb:142in `create_time_zone_conversion_attribute?'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/active_record/attribute_methods.rb:75:in `define_attributes_methods'
...
其他提示
我们前一段时间遇到类似你这样的问题。 UTC:据我记得,这可以通过在environment.rb中设置TIME_ZONE予以纠正。这是前一段时间,我不记得确切的配置参数的名称或者它是否是“UTC”或:UTC。试试看吧,或许它会帮助你。
不隶属于 StackOverflow