Why doesn't Rails autoload classes from app/services?

Ruby on-RailsAutoloadRuby on-Rails-4.2

Ruby on-Rails Problem Overview


I'm working on a Rails 4.2 app and have just added app/services/fetch_artists.rb to the structure. Inside this file, I have defined a class FetchArtists; end.

When trying to run rails r 'FetchArtists' it gives me a NameError: uninitialized constant FetchArtists.

I've tried looking at ActiveSupport::Dependencies.autoload_paths and indeed, app/services is not there:

/.../app/assets
/.../app/controllers
/.../app/helpers
/.../app/jobs
/.../app/mailers
/.../app/models
/.../app/controllers/concerns
/.../app/models/concerns
/.../spec/mailers/previews

My question is, why isn't this folder automatically loaded, and what should I do for it to be?

EDIT

Very strange, after repeatedly running the above command with rails runner, the new folder appears on the autoload paths. I have no idea why this happened with such a lag.

Someone suggested this may deal with spring. I would like to hear more on this, since it can possibly help many others in this situation too.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

I encountered the same problem and it seems to be a caching issue with Spring, a process which handles preloading your app. It's used for the web server as well as the console and Rake tasks.

Stopping Spring with bin/spring stop will force Spring to load your app fresh. Now running rails console and inspecting ActiveSupport::Dependencies.autoload_paths will successfully show app/services.

Solution 2 - Ruby on-Rails

In my case spring was not watching the app/services directory for changes - restarting Spring would load the class but changes to an existing class or new class would require a restart of Spring for them to apply.

To resolve this issue I added it to the list of directories watched by Spring in config/spring.rb:

%w(
  .ruby-version
  .rbenv-vars
  tmp/restart.txt
  tmp/caching-dev.txt
  app/services
).each { |path| Spring.watch(path) }

and restarted Spring one more time.

Solution 3 - Ruby on-Rails

I came with a similar problem, and took a quick glance at the Spring docs and found this bit about watchers.

I added the following to my application.rb and things fell into place -

Spring.watch "app/services/**"

I'm no expert here, ymmv.

Solution 4 - Ruby on-Rails

I was having the same problem, and found no solution. I'm not patient enough to wait for autoload to load it eventually, so my quick solution was to turn eager_load on, and start my server. It will finally load it. I switched it off afterwards and my classes were still loaded.

Just use: config.eager_load = true

in your config/environments/development.rb

Solution 5 - Ruby on-Rails

You should include it into autoload_paths:

config.autoload_paths += %W(#{Rails.root}/app/services)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlinkyndyView Question on Stackoverflow
Solution 1 - Ruby on-RailsmaknzView Answer on Stackoverflow
Solution 2 - Ruby on-RailsBrianView Answer on Stackoverflow
Solution 3 - Ruby on-RailssbauchView Answer on Stackoverflow
Solution 4 - Ruby on-RailsDaniel R CarlettiView Answer on Stackoverflow
Solution 5 - Ruby on-RailsdimakuraView Answer on Stackoverflow