gpt4 book ai didi

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

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

我编写了一个模块,它位于我的 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/environments/production.rb 中设置 config.cache_classes = false 那么我就不会无法获取堆栈跟踪,并且应用程序在生产模式下的行为与开发模式下的行为相同!!

如何设置 config.cache_classes = false 以这种方式在生产中包含一个模块?我是否以错误/奇怪的方式包含了该模块?

最佳答案

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

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

MyProject::Application.initialize!

加载需要的模块内容:

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

它包含在之后,它使整个事情正常进行。

在生产中,模块和类真的被加载了 => 崩溃了,它们需要它们的依赖!

结果,把:

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

在您在 config/initializers 中创建的初始化程序中,例如将其命名为 active_record_extension.rb

关于ruby-on-rails - 包括模块在开发中工作,在生产中启动时失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4840335/

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