Subfolders in lib

喜你入骨 提交于 2019-12-01 03:48:37

问题


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?


回答1:


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) }

It will require all ruby files in your lib folder. You just have to make sure if this is really what you want :)




回答2:


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.



来源:https://stackoverflow.com/questions/4981267/subfolders-in-lib

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