递归包含所有模型子目录 [英] Recursively including all model subdirectories

查看:46
本文介绍了递归包含所有模型子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何递归加载models和lib目录下的所有目录?在 application.rb 中,我有以下几行:

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

但他们似乎只添加了一层模型和 lib 子目录.

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

谢谢

推荐答案

这应该会有所帮助

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

享受吧!(:

更新:

很好的问题,上面发布的例子我只是提到了我最近的项目.

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.

主要区别当然不是File的join方法,不是config.root/Rails.root

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

'**' 后面的 '/' 是有道理的.

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

第一个在 globbing 时匹配only 目录.第二个谈话递归地.

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

这篇关于递归包含所有模型子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆