NameError: uninitialized constant on Heroku

Viewed 2449

I have a Rails 5 application with some modules/classes located under /lib. In development I can access those through the rails console like so:

irb(main):001:0> MyClass.do_something

In Production on Heroku I get this:

irb(main):001:0> MyClass.do_something
NameError: uninitialized constant MyClass

As you might have guessed I autoload the /lib directory in my application.rb:

config.autoload_paths << Rails.root.join('lib')

However, the most curious thing about this is, that I can access this class from rake tasks. So something like this works fine:

task do_something: :environment do
  MyClass.do_something
end

Which tells me that the class is present on Heroku.

Any ideas?

2 Answers

I have been puzzled why all of my objects were uninitialized constant in console on heroku in production. In my local production, they were fine.

Turns out that the problem was that I was running: "heroku run console" not "heroku run rails console".

Worth noting that when you access console from the heroku website, the same problem occurs there too. Wasted a lot of time on this.

Related