Recursively including all model subdirectories

柔情痞子 提交于 2019-12-03 12:08:02

问题


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('app', 'models', '{**}')]
config.autoload_paths += Dir[Rails.root.join('lib', '{**}')]

but they only seem to add one level of model and lib subdirectories.

Thanks


回答1:


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', '**/') ]


来源:https://stackoverflow.com/questions/7750769/recursively-including-all-model-subdirectories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!