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

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

问题描述

如何在模型目录和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.

第一个聊天时会说匹配目录. 第二个演讲是递归.

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天全站免登陆