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

166
Views
How to properly extend ApplicationRecord in Rails 6 with Zeitwerk

Consider a Rails 6 application that has app/models/application_record.rb. This Rails 6 application is using Zeitwerk loader.

class ApplicationRecord
end

If I want to add functionality to ApplicationRecord via a module:

# app/models/concerns/fancy_methods.rb
module FancyMethods
  def fancy_pants
    puts "I'm wearing fancy pants"
  end
end

and do the following:

class ApplicationRecord
  include FancyMethods
end

I will get a deprecation warning or error:

DEPRECATION WARNING: Initialization autoloaded the constant FancyMethods.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload FancyMethods, for example,
the expected changes wont be reflected in that stale Module object.

This autoloaded constant has been unloaded.

Please, check the "Autoloading and Reloading Constants" guide for solutions.
 (called from <top (required)> at /Users/peter/work/recognize/config/environment.rb:5)

I've read lots of articles including the Rails autoload docs, but nothing really addresses this minimal but common case of extending ApplicationRecord. Yes, I could wrap ApplicationRecord in a .to_prepare block like:

Rails.configuration.to_prepare do
  class ApplicationRecord < ActiveRecord::Base
    include FancyMethods
  end
end

But this seems like a code smell and could cause other unexpected problems now or down the line.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Figured it out! The issue was there was an explicit require in an initializer that loaded ApplicationRecord.

# config/initializers/setup_other_fancy_thing.rb
require 'application_record'

module OtherFancyThing
  def also_fancy
    puts 'also fancy'
  end
end

ApplicationRecord.send(:include, OtherFancyThing)

The way I debugged this was that I:

  1. Created a new Rails app of the same version and could not reproduce the error
  2. I copied the default application.rb and development.rb and still got the error
  3. Moved the entire config/initializers directory to a temp directory and it made the warning go away! So, I knew it had to be one of the initializers. From there it was just a matter of dividing and conquering until I found the offending initializer.
over 4 years ago · Santiago Trujillo Report

0

Great! So, basically, you do not need to define the module and include it that way. Idiomatically, you define the module in its own file, normal:

# app/models/other_fancy_thing.rb
module OtherFancyThing
  def also_fancy
    puts 'also fancy'
  end
end

and then

# app/models/application_record.rb
class ApplicationRecord
  include FancyMethods
end

Whenever ApplicationRecord is loaded, for example as a side-effect or loading some regular model, it will load FancyMethods just fine.

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!