【发布时间】:2019-07-30 17:48:37
【问题描述】:
我有一个看起来像这样的文件
#app/services/account/authenticate/base.rb
module Account
module Authenticate
AuthenticateError = Class.new(StandardError)
class Base < ::Account::Base
def self.call(*attrs)
raise NotImplementedError
end
end
end
end
现在当我从 rails c 运行代码时出现错误
> ::Account::Authenticate::AuthenticateError
=> NameError (uninitialized constant Account::Authenticate::AuthenticateError)
> ::Account::Authenticate.constants
=> [:Base, :ViaToken]
所以 rails 看不到 AuthenticateError 类。但是当我从这个文件夹中创建一个嵌套类时,就像
=> Account::Authenticate::ViaToken
> ::Account::Authenticate.constants
=> [:Base, :AuthenticateError, :ViaToken]
AuthenticateError 类现在可见
> ::Account::Authenticate::AuthenticateError
=> Account::Authenticate::AuthenticateError
这个问题的解决方案是创建一个单独的文件authenticate_error.rb,它可以从一开始就起作用,但这个解决方案对我来说并不理想。有没有办法预加载所有类或smth?
(Ruby 2.6 与 Rails 6.0.0.rc2)
【问题讨论】:
-
你试过在
config/environments/development中设置config.eager_load = true吗? -
@jvillian 它不起作用
-
您是否偶然使用了弹簧?
-
@jvillian 是的,spring 和所有 Rails 服务器在更改后停止/重新启动
-
即使没有春天我也经历过。但是,只有当我的模块位于非 railsy 目录并嵌套时才会发生这种情况。 IE。我可以添加
config.autoload_paths += Dir[Rails.root.join('app', 'service_objects', '{*/}')],它将包含 service_objects 中的所有内容。我还可以将模块添加到 /app/models 的子目录中。但是,对于嵌套在非 rails 目录中的文件,同样的方法不起作用。
标签: ruby-on-rails ruby