Rails 3如何在生产模式下加载模型层并解决模型依赖性?

时间:2023-01-16 09:39:23

In other words, does it work similarly to development mode but caches the classes as they are required? Or are all models loaded upfront?

换句话说,它是否与开发模式类似,但是在需要时缓存类?或者所有型号都预先加载?

If the latter, how does Rails know to load a model which is a dependency (of another model) before the model which depends on it?

如果是后者,Rails如何知道在依赖于它的模型之前加载一个依赖于(另一个模型的)依赖的模型?

I'd like to know so I can evaluate how feasible it is to load Rails models into a vanilla Ruby project without using the Rails script runner.

我想知道,所以我可以评估在不使用Rails脚本运行器的情况下将Rails模型加载到vanilla Ruby项目中的可行性。

1 个解决方案

#1


1  

The classes are required upfront. Rails basically does require_dependency on everything in config.eager_load_paths, in alphabetic order (see here).

这些课程是必需的。 Rails基本上对config.eager_load_paths中的所有内容执行require_dependency,按字母顺序排列(参见此处)。

require_dependency is part of Active Support, and is in a nutshell load/require but that integrates with Active Support's dependency tracking. If during this process rails comes across something which is not already loaded (e.g. if A was a subclass of B) then the usual const_missing hooks will fire and load b.rb.

require_dependency是Active Support的一部分,简而言之是load / require,但它与Active Support的依赖关系跟踪集成在一起。如果在此过程中rails遇到了尚未加载的内容(例如,如果A是B的子类),则通常的const_missing挂钩将触发并加载b.rb.

You should be able to setup Active Support like rails does and call the same methods from your non rails project.

您应该能够像rails一样设置Active Support,并从非rails项目中调用相同的方法。

#1


1  

The classes are required upfront. Rails basically does require_dependency on everything in config.eager_load_paths, in alphabetic order (see here).

这些课程是必需的。 Rails基本上对config.eager_load_paths中的所有内容执行require_dependency,按字母顺序排列(参见此处)。

require_dependency is part of Active Support, and is in a nutshell load/require but that integrates with Active Support's dependency tracking. If during this process rails comes across something which is not already loaded (e.g. if A was a subclass of B) then the usual const_missing hooks will fire and load b.rb.

require_dependency是Active Support的一部分,简而言之是load / require,但它与Active Support的依赖关系跟踪集成在一起。如果在此过程中rails遇到了尚未加载的内容(例如,如果A是B的子类),则通常的const_missing挂钩将触发并加载b.rb.

You should be able to setup Active Support like rails does and call the same methods from your non rails project.

您应该能够像rails一样设置Active Support,并从非rails项目中调用相同的方法。