This blog is part of our Rails 5 series.
Up to Rails 4.2, all models inherited from
But starting from Rails 5, all models will inherit from
What happened to
Well not much changed in reality. Following file will be automatically added to models in Rails 5 applications.
This behavior is similar to how controllers inherit from
instead of inheriting from
ApplicationRecord will be a single point of entry for all the customizations and
extensions needed for an application, instead of monkey patching
Say I want to add some extra functionality to Active Record. This is what I would do in Rails 4.2.
ActiveRecord::Base forever includes
MyAwesomeFeature and any class
inheriting from it also includes
MyAwesomeFeature even if they don’t want it.
This is especially true if you are using plugins and engines where monkey patching to
can leak into engine or plugin code.
ApplicationRecord, they will be localized to only those models which
are inheriting from
ApplicationRecord, effectively only to your application.
Migrating from Rails 4
By default all new Rails 5 applications will have
If you are migrating from Rails 4, then simply create
app/models/application_record.rb as shown below
and change all models to inherit from
ApplicationRecord instead of