【发布时间】:2011-08-10 14:07:46
【问题描述】:
这发生在 Rails 3.0.7 和 3.0.9、WEBrick 和 Apache 中。
我有一个模块Reportable,它有一个写inheritable_attribute的方法:
module Reportable
module ClassMethods
def add_report(report_name)
instance_eval do
write_inheritable_hash(:reportable_report_names,
{report_name => {:dates => true, :details => 'something'})
end
end
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
Reportable 被加载到 config/initializers 并且一个类使用它:
class User < ActiveRecord::Base
include Reportable
add_report :report1
add_report :report2
end
在生产模式下,服务器启动后第一次加载页面,属性加载正确:
User.read_inheritable_attribute(:reportable_report_names)
# => {:report1 => {:dates => true, :details => 'something'},
:report2 => {:dates => true, :details => 'something'}}
但是在第二页加载时:
User.read_inheritable_attribute(:reportable_report_names)
# => {:report1 => {:dates => true},
:report2 => {:dates => true}}
它在开发中按预期工作,在生产模式下在控制台中工作。该问题仅出现在生产模式下 Web 服务器上的 POST 请求中。什么给了?
【问题讨论】:
-
一种可能的解决方法是使类不可加载。在这里你可以找到它的解释:stackoverflow.com/questions/6853471/ruby-on-rails-unloadable
标签: ruby-on-rails metaprogramming