每个请求擦除可防止重新加载某些类

时间:2013-08-15 21:48:25

标签: ruby-on-rails ruby-on-rails-4

如果在服务器以开发模式运行时检测到文件更改,则Rails会擦除类并重新加载它们。我有以下结构:

app/models/algorithms/algorithms.rb (Module Algorithms)

app/models/algorithms/algorithm.rb (Base Class Algorithms::Algorithm)

app/models/algorithms/algorithm_a.rb (Extended Classes Algorithms::AlgorithmA)
app/models/algorithms/algorithm_b.rb (Extended Classes Algorithms::AlgorithmB)
app/models/algorithms/algorithm_c.rb (Extended Classes Algorithms::AlgorithmC)

这些具体的算法类不是由控制器实例化的,而是在Sidekiq工作类app/workers中实现,这通常不在Rails过程中,而是在Sidekiq过程中。

现在,如果我对任何文件(例如控制器)进行更改,则Rails会擦除类并重新加载它们。这很好,但它不会重新加载整个app/models/algorithms目录。

为什么会这样?我怎样才能将其配置为每次都急切地加载所有内容?我已将config.eager_load设置为true。直到我在一个pry会话中一个接一个地引用每个类,然后再一次加载,一个接一个地解决问题。

1 个答案:

答案 0 :(得分:1)

查看http://edgeguides.rubyonrails.org/configuring.html处的文档,您似乎可以为自动加载添加自定义路径。

我引用:

  
      
  • config.eager_load为true时,eager加载所有已注册的config.eager_load_namespaces。这包括您的应用程序,引擎,Rails框架和任何其他已注册的命名空间。

  •   
  • config.eager_load_namespaces注册config.eager_load为true时急切加载的名称空间。列表中的所有命名空间都必须响应eager_load!方法

  •   

我觉得有趣的是以下内容。

  
      
  • config.eager_load_paths接受一系列路径,如果启用了缓存类,Rails将在启动时急切加载。默认为应用程序的app目录中的每个文件夹。
  •   

对我来说,这也会适当地加载你的文件。

我尝试将以下内容添加到config/application.rb

config.eager_load_paths += Rails.root.join('app/models/algorithms')

注意:仅当cache_classes设置为true时才有效。

您也可以急切加载命名空间,这可以通过执行以下操作来完成:

config.config.eager_load_namespaces += Algorithms

然后实现一个正确处理设置的方法eager_load!

相关问题