In Rails, there is a lib directory. It is not autoloaded by default. So when you load the Rails environment, all the modules and classes in this folder will not be known at runtime. I tried to autoload lib in the application.rb
:
在Rails中,有一个lib目录。默认情况下不会自动加载。因此,当您加载Rails环境时,将无法在运行时知道此文件夹中的所有模块和类。我试图在application.rb中自动加载lib:
config.autoload_paths += %W(#{config.root}/app/models/ckeditor #{config.root}/lib)
But it does not work. It does not load the modules and classes in lib I want loaded.
但它不起作用。它不会加载我想要加载的库中的模块和类。
File structure is as so:
文件结构如下:
\lib
\docket
\practice_factory
factory.rb
pa_factory.rb
I have this in factory.rb:
我在factory.rb中有这个:
module Docket
module PracticeFactory
class Factory
def self.practice_methods
descendants.collect {|descendant| descendant.practice_methods }
end
end
end
end
And in pa_factory.rb:
在pa_factory.rb中:
module Docket
module PracticeFactory
class PaFactory < Factory
def self.practice_methods
{
some: 'data',
more: 'stuff'
}
end
end
end
end
When I load rails console, and I get empty array when I run:
当我加载rails控制台时,我运行时得到空数组:
Docket::PracticeFactory::Factory.practice_methods
=> []
The reason why array is empty is because the PaFactory is not loaded in memory. This is despite the fact that I included lib to autoload path! So why is not not autoloaded #1?
数组为空的原因是因为PaFactory未加载到内存中。尽管事实上我将lib包含在自动加载路径中!那么为什么不自动加载#1呢?
Also, I added the following to factory.rb:
另外,我在factory.rb中添加了以下内容:
Dir["#{Rails.root}/lib/docket/practice_factory/*_factory.rb"].each {|file| require file }
And it gives me error:
它给了我错误:
RuntimeError: Circular dependency detected while autoloading constant Docket::PracticeFactory::Factory
RuntimeError:自动加载常量Docket :: PracticeFactory :: Factory时检测到循环依赖性
Interesting error, I am loading files with *_factory.rb and factory.rb does not have an underscore, but those other files do inherit from Factory. How can this error be avoided?
有趣的错误,我正在使用* _factory.rb加载文件,而factory.rb没有下划线,但那些其他文件确实从Factory继承。如何避免这种错误?
1 个解决方案
#1
1
I have nothing to say about autoloading, the magic never worked for me and I try to avoid it wherever possible.
我对自动加载没什么可说的,魔术从来没有对我有用,我试图尽可能避免它。
For the latter, the error cause is clear: you try to load PbFactory
that requires Factory
class to be defined (since it derives from it,) and it is not yet defined. To resolve an issue just put your
对于后者,错误原因很明显:您尝试加载需要定义Factory类的PbFactory(因为它派生自它),并且尚未定义。要解决问题,请把你的
Dir["*_factory.rb"].each {|file| require_relative file }
after the Factory
class declaration.
在Factory类声明之后。
#1
1
I have nothing to say about autoloading, the magic never worked for me and I try to avoid it wherever possible.
我对自动加载没什么可说的,魔术从来没有对我有用,我试图尽可能避免它。
For the latter, the error cause is clear: you try to load PbFactory
that requires Factory
class to be defined (since it derives from it,) and it is not yet defined. To resolve an issue just put your
对于后者,错误原因很明显:您尝试加载需要定义Factory类的PbFactory(因为它派生自它),并且尚未定义。要解决问题,请把你的
Dir["*_factory.rb"].each {|file| require_relative file }
after the Factory
class declaration.
在Factory类声明之后。