How can I preload concerns in a rails initializer using Rails 6/Zeitwerk?

后端 未结 2 1312
忘掉有多难
忘掉有多难 2021-01-07 23:27

I\'m working with an initializer that does some monkey patching on app start by including some app concerns into a third party lib. Basically:

# config/initi         


        
2条回答
  •  借酒劲吻你
    2021-01-07 23:53

    Would help if I read the error message a bit more closely:

    Autoloading during initialization is going to be an error condition in future versions of Rails.

    Discussion for the change is here and guide is here.

    In short, autoloading shouldn't be done in initializers, and this is going to be phased out. Solutions are to either 1) Don't use stuff that needs to be autoloaded in initializers (preferred, obviously), or 2) explicitly require dependencies in initializers.

    So I would do:

    # config/initializers/my_initializer.rb
    
    require 'my_concern1'
    require 'my_concern2'
    
    class SomeExternalLib
      include MyConcern1
      include MyConcern2
    end
    

提交回复
热议问题