包括模块在开发中工作,在生产启动时失败?

时间:2011-01-30 00:18:14

标签: ruby-on-rails ruby ruby-on-rails-3

我已经创作了一个存放在lib文件夹中的模块。我在启动时把它包含在我的environment.rb文件中,如下所示

# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
MyProject::Application.initialize!

ActiveRecord::Base.send :include, MyProject::Has::Formatter
include MyProject::EventFormatters

这在开发中非常有用,但如果我尝试在生产模式下运行,或者如果我将我的应用程序部署到生产环境,那么当服务器尝试启动时,我会得到以下堆栈跟踪:

=> Booting WEBrick
=> Rails 3.0.3 application starting in production on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing': undefined local variable or method `has_formatter' for #<Class:0x808f830> (NameError)
        from C:/Data/myproject/app/models/event.rb:2
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:227:in `load_dependency'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:346:in `require_or_load'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:300:in `depend_on'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:216:in `require_dependency'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:138:in `eager_load!'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `each'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `eager_load!'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `each'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `eager_load!'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:108:in `eager_load!'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application/finisher.rb:41
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `run_initializers'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `send'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
        from C:/Data/myproject/config/environment.rb:5
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
        from C:/Data/myproject/config.ru:3
        from C:/Ruby187/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
        from C:/Ruby187/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
        from C:/Data/myproject/config.ru:1:in `new'
        from C:/Data/myproject/config.ru:1

现在这里是整个事情中令人难以置信的一部分:如果我在config.cache_classes = false中设置config/environments/production.rb,那么我没有获得该堆栈跟踪,并且应用程序在生产模式下的行为与它相同开发模式!!

设置config.cache_classes = false使得包含这种方式的模块在生产中有用吗?我是否以错误/怪异的方式包含模块?

2 个答案:

答案 0 :(得分:4)

希望我完全,对,我至少会提出我的观点:

在开发中,所有类都是延迟加载的。这样,您可以轻松更改它们,因为它们仅在必要时加载。这意味着:

MyProject::Application.initialize!

加载需要的模块内容:

ActiveRecord::Base.send :include, MyProject::Has::Formatter
include MyProject::EventFormatters

后面包含 ,它使整个过程发挥作用。

在生产中,模块和类确实被加载=&gt;崩溃,他们需要他们的依赖!

结果,放:

ActiveRecord::Base.send :include, MyProject::Has::Formatter
include MyProject::EventFormatters

在您在config/initializers中创建的初始值设定项中,例如将其称为active_record_extension.rb

答案 1 :(得分:0)

自动加载对您的方案不起作用吗?

# config/application.rb
config.autoload_paths += %W(#{config.root}/lib)

# lib/my_project/event_formatters.rb
module MyProject
  module EventFormatters
    # .. etc. ...
  end
end