22

How do you load all directories recursively in the models and lib directories? In application.rb, I have the lines:

config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]
config.autoload_paths += Dir[Rails.root.join('lib', '{**}')]

but they only seem to add one level of model and lib subdirectories.

Thanks

GTDev
  • 5,488
  • 9
  • 49
  • 84

1 Answers1

41

this should be helpful

 Dir["#{config.root}/app/models/**/","#{config.root}/lib/**/"]

enjoy! (:

Update:

Excellent question, posting example above i have simply referred to my recent project.

After making some tests, better understanding comes to me and it is great.

The main difference is of course neither in join method of File not config.root / Rails.root

Trailing '/' after '**' makes sense.

First one talks to match only directories when globbing. Second one talks do it recursively.

In your case this one could be also appropriate

Dir[ Rails.root.join('app', 'models', '**/') ]
sarvavijJana
  • 1,212
  • 10
  • 11
  • 1
    Nice that worked... a little confused how `Dir[Rails.root.join('app', 'models', '{**}')]` is different from `Dir["#{config.root}/app/models/**/"]`. Thanks though – GTDev Oct 13 '11 at 07:42
  • i've updated my answer with some explanation as it was big enough – sarvavijJana Oct 13 '11 at 08:29
  • 1
    I still don't get it. Then why would anybody use '{**}' instead of ' **/' ? – jgomo3 Jun 12 '17 at 22:57