1

I want to add custom class to app folder into clean Rails 6 project, ruby 2.7.0. According to Zeitwerk documentation it should auto-load all paths under app directory.

app/custom/car.rb
class Car
  def self.print
    print 'Car'
  end  
end

I'm getting error in a rails console

2.7.0 :001 > Car.print
NameError (uninitialized constant Car)

What is wrong?

Note: when I put car.rb to app/models it works fine, but not when it's inside app/custom directory

  • Have you looked into https://stackoverflow.com/questions/58681673/i-moved-my-folder-from-lib-to-app-in-a-rails-6-i-cant-seem-to-instantiate-an? – Olkin May 11 '20 at 14:47
  • All the directories under `app` should load automatically. Try `spring stop` and restart the `rails serer` – Amit Patel May 11 '20 at 14:47

1 Answers1

2

Your issue is likely not with the Zeitwerk loader, but with the Spring preloader that runs by default - https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-paths notes that "By default, the autoload paths of an application consist of all the subdirectories of app that exist when the application boots"

That "when the application boots" note is very important - with spring in play, if you add a folder to /app and spring is already running, you'll have to restart the spring preloader to reboot the app in the background so Zeitwerk will pick it up.

bundle exec spring status will show you the status of the spring preloader, and you can use bundle exec spring stop to stop it and force it to reload the next time you run a command that would use it like rails console

jimcavoli
  • 854
  • 9
  • 22