5

I'm attempting to upgrade from Rails 5 to 6. I did the upgrade steps including adding this:

# config/application.rb
config.load_defaults 6.0

I have this class:

# lib/notification/auto_thank.rb
module Notification
  class AutoThank
    def perform
      # stuff
    end
  end
end

Which is used in a task:

namespace :notify do
  task auto_thank: :environment do
    Notification::AutoThank.new.perform
  end
end

When I do puts config.autoload_paths, it's listed, so I expect it to autoload:

/my/app/path/lib/notification/auto_thank.rb

But when I run the task I get an error:

NameError: uninitialized constant Notification

It gets stranger. When I add a require to the task:

task auto_thank: :environment do
  require '/my/app/path/lib/notification/auto_thank.rb'
  Notification::AutoThank.new.perform
end

I get a different error:

NameError: expected file /my/app/path/lib/notification/auto_thank.rb to define constant AutoThank, but didn't

What am I missing?

Tim Scott
  • 15,106
  • 9
  • 65
  • 79

1 Answers1

2

If you can, locate your auto-loading code in app/. As of Rails 6 everything in there will be automatically part of the auto-load path. Rails 5 was more selective.

In this case call it: app/notifications/notification/auto_thank.rb

The first part of the path is ignored. The second (optional) part is the namespace.

Note that to get this to show up in the auto-loader you may need to stop the Spring process with spring stop. This must be done any time you introduce a new app/... path.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • I would assume that the same issue will happen for everything in lib. I don't think I want to move my whole lib into app. There must be a way to autoload from lib, right? – Tim Scott Oct 28 '19 at 22:07
  • 1
    You can add it to the auto-load path but the best approach is to put it into `app/` where it belongs. The old `lib/` approach is bound to be deprecated soon and is [not without problems](https://stackoverflow.com/questions/38198668/rails-5-load-lib-files-in-production). – tadman Oct 28 '19 at 22:40