Rails 6 自动加载未找到类

Tim*_*ott 5 ruby ruby-on-rails-6 zeitwerk

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

# config/application.rb
config.load_defaults 6.0
Run Code Online (Sandbox Code Playgroud)

我有这门课:

# lib/notification/auto_thank.rb
module Notification
  class AutoThank
    def perform
      # stuff
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

在任务中使用的是:

namespace :notify do
  task auto_thank: :environment do
    Notification::AutoThank.new.perform
  end
end
Run Code Online (Sandbox Code Playgroud)

当我这样做时puts config.autoload_paths,它会被列出,所以我希望它能够自动加载:

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

但是当我运行任务时出现错误:

NameError:未初始化的常量通知

它变得陌生了。当我向任务添加要求时:

task auto_thank: :environment do
  require '/my/app/path/lib/notification/auto_thank.rb'
  Notification::AutoThank.new.perform
end
Run Code Online (Sandbox Code Playgroud)

我收到一个不同的错误:

NameError:预期文件 /my/app/path/lib/notification/auto_thank.rb 定义常量 AutoThank,但没有

我缺少什么?

tad*_*man 3

如果可以的话,请在app/. 从 Rails 6 开始,其中的所有内容都将自动成为自动加载路径的一部分。Rails 5 更具选择性。

在这种情况下调用它:app/notifications/notification/auto_thank.rb

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

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