未初始化常量 ActiveRecord::DeleteRestrictionError 仅在部署到 Heroku 时
uninitialized constant ActiveRecord::DeleteRestrictionError only when deployed to Heroku
我的 Rails 应用程序在 Heroku 上加载时出现未初始化常量错误,但它在开发中运行良好。
Heroku 日志(启动时中断):
/app/app/controllers/application_controller.rb:19:in `<class:ApplicationController>': uninitialized constant ActiveRecord::DeleteRestrictionError (NameError)
controllers/application_controller.rb
中的相关行:
class ApplicationController < ActionController::API
# ...
# Line 19
rescue_from ActiveRecord::DeleteRestrictionError, with: :not_processable
# ...
end
如果我注释掉 rescue_from
,如果记录由于 restrict_with_error 依赖关系而无法保存,我会从未捕获的异常中得到内部服务器错误。如果我从中拯救出来,那么服务器将无法启动,但只能在生产环境中启动。
我猜这与 eager load 有关 and/or zeitwerk 加载常量的方式发生了变化,但我还没有找到任何关于如何解决这个问题的答案。
我在 2008 年发现了一个类似的问题/解决方案:
https://discuss.rubyonrails.org/t/rescue-from-issue-with-aws-uninitialized-constant/28759/2
他们的解决方案是将常量包装在一个字符串中以避免加载时出现文件解析时间问题,这似乎也适用于这种情况:
class ApplicationController < ActionController::API
# ...
rescue_from "ActiveRecord::DeleteRestrictionError", with: :not_processable
# ...
end
此问题已通过最小应用程序重现,并在 Github 上打开了一个问题:https://github.com/rails/rails/issues/43666
我的 Rails 应用程序在 Heroku 上加载时出现未初始化常量错误,但它在开发中运行良好。
Heroku 日志(启动时中断):
/app/app/controllers/application_controller.rb:19:in `<class:ApplicationController>': uninitialized constant ActiveRecord::DeleteRestrictionError (NameError)
controllers/application_controller.rb
中的相关行:
class ApplicationController < ActionController::API
# ...
# Line 19
rescue_from ActiveRecord::DeleteRestrictionError, with: :not_processable
# ...
end
如果我注释掉 rescue_from
,如果记录由于 restrict_with_error 依赖关系而无法保存,我会从未捕获的异常中得到内部服务器错误。如果我从中拯救出来,那么服务器将无法启动,但只能在生产环境中启动。
我猜这与 eager load 有关 and/or zeitwerk 加载常量的方式发生了变化,但我还没有找到任何关于如何解决这个问题的答案。
我在 2008 年发现了一个类似的问题/解决方案:
https://discuss.rubyonrails.org/t/rescue-from-issue-with-aws-uninitialized-constant/28759/2
他们的解决方案是将常量包装在一个字符串中以避免加载时出现文件解析时间问题,这似乎也适用于这种情况:
class ApplicationController < ActionController::API
# ...
rescue_from "ActiveRecord::DeleteRestrictionError", with: :not_processable
# ...
end
此问题已通过最小应用程序重现,并在 Github 上打开了一个问题:https://github.com/rails/rails/issues/43666