Search code examples
ruby-on-railsrubyload-path

Rails: load_paths for directory and all subdirectories


In environment.rb I can add the line

config.load_paths += %W( #{RAILS_ROOT}/app/models/foos )

where 'foos' is a folder. This loads all files inside the foos folder. However it doesn't load any of its subdirectories.

If I knew the names of all the subdirectories in the 'foos' folder, this problem would have an easy solution:

%W[folder1 folder2 folder2].each { |f| f.config.load_paths += %W( #{RAILS_ROOT}/app/models/foos/#{f} ) }

However, I won't always know the names of all folders inside of 'foos'. Is there someway to do this:

config.load_paths += %W( #{RAILS_ROOT}/app/models/foos/#{**WILDCARD**} )

Thanks


Solution

  • It looks like this other question has the type of solution you are looking for to get all the sub directories:

    get all of the immediate subdirectories in ruby

    You can use something like the following to point at a particular directory and get a list of all the sub directories of it:

    Dir['/home/username/Music/*/']
    

    This will return an array of all the paths to the sub directories of the Music folder.