Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5: Autoloading with custom folders not working

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.

like image 861
ascherman Avatar asked Apr 28 '17 21:04

ascherman


1 Answers

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
like image 129
max Avatar answered Nov 16 '22 17:11

max