Rails force models to eager load

后端 未结 4 1428
我寻月下人不归
我寻月下人不归 2020-12-30 22:30

I would like to be able to load an entire app so that I may find the descendants of a given class.

For example given I have the following class defined:



        
4条回答
  •  梦谈多话
    2020-12-30 23:10

    From Configuring Rails Applications

    1. config.eager_load when true Eager loads all registered config.eager_load_namespaces. This includes your application, engines, Rails frameworks and any other registered namespace.
    2. config.eager_load_namespaces registers namespaces that are eager loaded when config.eager_load is true. All namespaces in the list must respond to the eager_load! method.
    3. config.eager_load_paths accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the app directory of the application.

    EDIT:

    To manually load you should be able to do something like:

    matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/
    Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
      require_dependency file.sub(matcher, '\1')
    end
    

提交回复
热议问题