naughie
naughie

Reputation: 315

Added to autoload paths but not loaded

I am using Rails 5.1.6 on AWS EC2 t2.micro.

I have a file, say lib/foo/bar.rb, where the class

class Foo::Bar
end

is defined. I added

config.autoload_paths += %W(#{config.root}/lib)

to config/application.rb, but when I check the constant Foo::Bar is loaded from rails console, it raises the uninitialized constant Foo::Bar Name error.

So, I added lines

puts "CONFIG ROOT IS #{config.root};"
puts "AUTOLOAD PATHS ARE #{config.autoload_paths};"

at the end of config/application.rb in order to see if they are correctly set, and they are right.

Then, I tried require './lib/foo/bar.rb'; Foo::Bar in rails console, and it is also OK.

The attempts at (local) docker are also good.

What is the problem? Why the constants in lib are not loaded?

Upvotes: 0

Views: 215

Answers (1)

Raj
Raj

Reputation: 22926

Have you tried:

module Foo
  class Bar
  end
end

https://dan.chak.org/enterprise-rails/chapter-3-organizing-with-modules/

Upvotes: 1

Related Questions