Rails 5: Autoloading with custom folders not working
ruby, ruby-on-rails
Solution
module Sandbox
class Application < Rails::Application
config.autoload_paths += [config.root.join('app')]
end
end
This will let Rails autoload `Events::NewRequest` from `app/events/new_request.rb`.
irb(main):001:0> Events
=> Events
irb(main):002:0> Events::NewRequest
=> Events::NewRequest
Problem
I have this class: ``` # app/events/new_request.rb class Events::NewRequest end ``` And I added that folder to the autoload: ``` # config/application.rb config.autoload_paths += %W( events/ ) ``` And when running `rails c`: ``` > Events::NewRequest NameError: uninitialized constant Events ``` The thing is that if I don't use the namespace "Events" when defining the class, it autoloads the class successfully.