gpt4 book ai didi

ruby-on-rails - 如何在关闭 cache_classes 的情况下使用来自中间件的域对象?

转载 作者:数据小太阳 更新时间:2023-10-29 07:08:22 24 4
gpt4 key购买 nike

在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 延迟绑定(bind)到类,直到方法调用时间,但这将问题变成了一个新问题:

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是解决问题的提交。原始错误报告是 here .

关于ruby-on-rails - 如何在关闭 cache_classes 的情况下使用来自中间件的域对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/991113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com