Rails 6自动加载找不到类



我正在尝试从Rails 5升级到Rails 6。我做了升级步骤,包括添加以下内容:

# config/application.rb
config.load_defaults 6.0

我有这个班:

# lib/notification/auto_thank.rb
module Notification
class AutoThank
def perform
# stuff
end
end
end

用于任务:

namespace :notify do
task auto_thank: :environment do
Notification::AutoThank.new.perform
end
end

当我执行puts config.autoload_paths时,它会被列出,所以我希望它自动加载:

/my/app/path/lib/notification/auto_thank.rb

但当我运行任务时,我会遇到一个错误:

名称错误:未初始化常量通知

它变得更奇怪了。当我向任务添加需求时:

task auto_thank: :environment do
require '/my/app/path/lib/notification/auto_thank.rb'
Notification::AutoThank.new.perform
end

我得到一个不同的错误:

名称错误:需要文件/my/app/path/lib/notification/auto_thank.rb来定义常量AutoThank,但没有

我缺少什么?

如果可以,请在app/中找到您的自动加载代码。从Rails6开始,那里的所有东西都将自动成为自动加载路径的一部分。Rails5更具选择性。

在这种情况下,称之为:app/notifications/notification/auto_thank.rb

路径的第一部分将被忽略。第二部分(可选(是命名空间。

请注意,要使其显示在自动加载器中,您可能需要使用spring stop停止Spring进程。每当引入新的app/...路径时,都必须执行此操作。

最新更新