6

在 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'
  ...
4

2 回答 2

2

这似乎是一个 Rails 错误。看看是否可以将 Rails 版本升级到 2.3.4 或 2.3.5。

我相信是解决问题的提交。原始错误报告在这里

于 2010-01-10T22:47:22.947 回答
-1

我们前段时间遇到了与您类似的问题。据我记得,这可以通过将 environment.rb 中的 time_zone 设置为 :utc 来解决。那是前一阵子,我不记得确切的配置参数名称,也不记得它是“UTC”还是:utc。试试看,也许会有所帮助。

于 2010-01-14T00:03:42.067 回答