lib中的子文件夹 [英] Subfolders in lib
问题描述
我有一个名为user_searches的模块.它执行的一些搜索不是用户模型的核心,因此,为什么我要把责任放在其他地方.我想组织所有这样的模型,这些模型在名为user的lib子文件夹中执行非核心用户功能.现在,我必须将模块的方法包括在User模型中...
I have a module called user_searches. It performs some searches that aren't core to the user model, thus, why I'm putting the responsibility somewhere else. I want to organize all my models like this that perform non-core user functions in a lib subfolder called user. Right now to include the module's methods in the User model I have to put...
require 'user/user_searches'
class User < ActiveRecord::Base
include UserSearches
end
...如果文件直接位于lib文件夹中,则不需要,但如果位于子文件夹中,则不需要.我该怎么办,所以不需要 require ?
...I don't need the require if the file is directly in the lib folder, but do if it's in the subfolder. What do I have to do so I don't need the require?
推荐答案
您可以将必要的要求行放入lib/user.rb
中,所有要求均在应用程序启动时递归加载.
You could put the necessary require lines into lib/user.rb
that way, all requirements are loaded recursively on application launch.
或者,您可以将类似以下内容放入初始化程序中:
Alternatively, you could put something like this into an initializer:
# put into config/initializers/load_lib.rb
Dir["#{RAILS_ROOT}/lib/**/*.rb"].each { |f| require(f) }
这将需要lib文件夹中的所有ruby文件.您只需要确定这是否真的是您想要的:)
It will require all ruby files in your lib folder. You just have to make sure if this is really what you want :)
这篇关于lib中的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!