Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Rails 5 autoloading issue in production

I have a hard time understanding an issue with Rails autoloading.

So I have a controller which is located in a folder named api. The constroller is declared as class Api::TestController.

In another folder, I have a file called app/my_folder/service_name/api.rb

In production, I get this error : load_missing_constant': Unable to autoload constant Api, expected /app/my_folder/service_name/api.rb to define it (LoadError)

application.rb has this line :

config.autoload_paths += Dir[Rails.root.join('app', 'my_folder', '{**/}')]

I have no issues in development.

Why is this happening ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Its happening since the auto load paths are only used for auto loading. In order for your class to be loaded in production you need to augment the eager_load_paths as well:

config.eager_load_paths += Dir[Rails.root.join('app', 'my_folder', '{**/}')]

However this isn't exactly a great practice - any directory in app is automatically used as a root directory. What you're doing is adding every subdirectory of a given directory as root directories.

Using the scope resultion operator (class Api::TestController) for namespace definition should also be avoided as it leads to suprising constant lookup behavior and is the source of autoloading bugs. Instead you should nest the class explicitly and setup an inflection for the acronym:

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'API'
end

# every time you use Api as a constant name a kitten dies.
module API
  class TestController
    puts Module.nesting.inspect # [TestController::API, API] 
  end
end 
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!