Subfolders in lib

I have a module called user_searches. It performs some search queries that are not the basis for the user model, so I put the responsibility elsewhere. I want to organize all my models, like these, that perform non-core user functions in a lib subfolder called a user. Right now, to include module methods in the User model, I need to put ...

require 'user/user_searches' class User < ActiveRecord::Base include UserSearches end 

... I do not need a request if the file is located directly in the lib folder, but do it in a subfolder. What do I need to do, I do not need a request?

+5
source share
2 answers

You can put the required query lines in lib/user.rb in this way, all requirements are loaded recursively when the application starts.

Alternatively, you can put something like this in the initializer:

 # put into config/initializers/load_lib.rb Dir["#{RAILS_ROOT}/lib/**/*.rb"].each { |f| require(f) } 

This will require all the ruby ​​files in your lib folder. You just need to make sure that this is really what you want :)

+3
source

These are works that cause

in the rails-2.2.2 / lib / initializer.rb file in the default_load_paths method is initialized to load the path only to the lib folder without subdirectories, in order to solve this problem, you can edit the project project.rb configuration file and click on the array of all subdirectories in config.load_path .

0
source

Source: https://habr.com/ru/post/893530/


All Articles