All posts

Fix Missing Import in Rails

Resolve NameError and LoadError in Rails applications, covering autoloading, Zeitwerk conventions, and gem dependency issues.

Missing Import Errors in Rails

Rails uses Zeitwerk for autoloading, which means you rarely need explicit require statements. When you see NameError: uninitialized constant X, something is misconfigured.

Zeitwerk Naming Conventions

Zeitwerk expects class names to match file paths:

# app/models/user.rb → User
# app/services/payment_processor.rb → PaymentProcessor
# app/services/stripe/charge_service.rb → Stripe::ChargeService

# BAD — file name doesn't match class
# app/services/stripeChargeService.rb → expects StripeChargeService
# but you defined Stripe::ChargeService

Check Autoload Paths

# config/application.rb
config.autoload_paths += %W[#{config.root}/lib]
config.eager_load_paths += %W[#{config.root}/lib]

Common Errors

Constant not found in development but works in production (or vice versa):

# Check for naming issues
bin/rails zeitwerk:check

Gem class not available:

# Gemfile — make sure it's not in the wrong group
gem 'sidekiq'  # Available everywhere

# NOT:
group :development do
  gem 'sidekiq'  # Won't be available in production!
end

Explicit Require

For code outside autoload paths:

# For non-autoloaded files
require 'csv'
require 'net/http'
require_relative '../lib/custom_parser'

Concerns and Modules

# app/models/concerns/searchable.rb
module Searchable
  extend ActiveSupport::Concern
  # ...
end

# app/models/product.rb
class Product < ApplicationRecord
  include Searchable  # Autoloaded from concerns/
end

Bugsly captures NameError and LoadError in Rails with the constant name and backtrace, showing exactly where the missing reference was triggered.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free