lib 中的子文件夹
我有一个名为 user_searches 的模块。它执行一些不是用户模型核心的搜索,因此,为什么我将责任放在其他地方。我想像这样组织我的所有模型,这些模型在名为 user 的 lib 子文件夹中执行非核心用户功能。现在,要将模块的方法包含在用户模型中,我必须......
require 'user/user_searches'
class User < ActiveRecord::Base
include UserSearches
end
如果文件直接位于 lib 文件夹中,则不需要 require,但如果文件位于子文件夹中,则需要。我必须做什么才能不需要要求?
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
...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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将必要的 require 行放入 lib/user.rb 中,这样所有需求都会在应用程序启动时递归加载。
或者,您可以将类似的内容放入初始化程序中:
它将需要 lib 文件夹中的所有 ruby 文件。您只需确定这是否真的是您想要的:)
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:
It will require all ruby files in your lib folder. You just have to make sure if this is really what you want :)
这是导致
文件 Rails-2.2.2/lib/initializer.rb 中方法 default_load_paths 初始化为仅加载 lib 文件夹而没有子目录的路径的作品,要解决此问题,您可以编辑项目 `environment.rb 配置文件并推入配置.load_path 数组所有子目录。
This is works that cause
in file rails-2.2.2/lib/initializer.rb in method default_load_paths initialized to load path just the lib folder without subdirectories, to solve this you can edit your project ` environment.rb config file and push into config.load_path array all subdirs.