Recursively including all model subdirectories

后端 未结 1 1478
无人共我
无人共我 2021-02-05 07:28

How do you load all directories recursively in the models and lib directories? In application.rb, I have the lines:

config.autoload_paths += Dir[Rails.root.join         


        
相关标签:
1条回答
  • 2021-02-05 08:09

    this should be helpful

     Dir["#{config.root}/app/models/**/","#{config.root}/lib/**/"]
    

    enjoy! (:

    Update:

    Excellent question, posting example above i have simply referred to my recent project.

    After making some tests, better understanding comes to me and it is great.

    The main difference is of course neither in join method of File not config.root / Rails.root

    Trailing '/' after '**' makes sense.

    First one talks to match only directories when globbing. Second one talks do it recursively.

    In your case this one could be also appropriate

    Dir[ Rails.root.join('app', 'models', '**/') ]
    
    0 讨论(0)
提交回复
热议问题