Rails console: Unable to autoload constant
It seems like a case of messed up naming convention.
As per Rails
naming convention, file names should be in snake_case
and class names in CamelCase
. In your scenario, the file name should be customer_rating.rb
and class name should be CustomerRating
.
After making these changes, use CustomerRating.all
(as the updated class name is CustomerRating
) to fetch all the customer ratings. Do not use Customer_rating.all
.
I'd also like to add a scenario of this problem that I found for future reference.
I'm running Rails 4.0 and I had this same problem but what happened was I had a model named Student inside student.rb that was contained in a folder called Student. I didn't realize it at first but the folder name was the problem. Changing the folder name to something other than a model name solved the problem.
If the naming convention is not off, like in this question, it may be an issue on initial first load if you're making a lot of requests at the same time. I experienced this with nested controllers Api::LocationsController.
I solved it by enabled eager_load in development env:
Rails.application.configure do
...
# Enabled this to avoid crash unable to autoload controller
# Error happens when you start and stop server on initial requests
# solution found via https://github.com/rails/rails/issues/32082#issuecomment-367715194
config.eager_load = true
I based this off of rails issues comments: https://github.com/rails/rails/issues/32082#issuecomment-367715194