如何在不使用cache_classes的情况下使用来自中间件的域对象? [英] How do I use domain objects from middleware with cache_classes off?
问题描述
在Rails开发环境中,cache_classes
已关闭,因此您可以在app/
下修改代码并查看更改,而无需重新启动服务器.
In the rails development environment, cache_classes
is off so you can modify code under app/
and see changes without restarting the server.
尽管如此,在所有环境中,中间件仅创建一次.因此,如果我有这样的中间件:
In all environments, though, middleware is only created once. So if I have middleware like this:
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
然后我将始终出现以下错误:
then I'll always get the following error:
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
类在每次调用时都会重新加载.
The problem is that the MyMiddleware
instance is created once at system load time, but the MyModel
class is reloaded on each call.
我尝试'MyModel'.constantize.first
将绑定到类的时间延迟到方法调用时,但这将问题更改为一个新的问题:
I tried 'MyModel'.constantize.first
to delay binding to the class until method-call-time, but that changes the problem to a new one:
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'
...
推荐答案
这似乎是Rails的错误.查看是否可以将Rails版本升级到2.3.4或2.3.5.
This appears to be a Rails bug. See if you can upgrade your Rails version to 2.3.4 or 2.3.5.
I believe this is the commit that fixed the problem. Original bug report is here.
这篇关于如何在不使用cache_classes的情况下使用来自中间件的域对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!