Why Rails 5 uses ApplicationRecord instead of ActiveRecord::Base?

Hieu Pham :

We know that Rails 5 added ApplicationRecord as an abstract class which was inherited by our models (ActiveRecord).

But basically, I think every technical requirement we do with ApplicationRecord, we can also do with ActiveRecord::Base. For instance:

module MyFeatures
  def do_something
    puts "Doing something"
  end
end

class ApplicationRecord < ActiveRecord::Base
  include MyFeatures
  self.abstract_class = true
end

So now every model will be attached the behaviors of MyFeatures. But we can also achieve this in Rails 4:

ActiveRecord::Base.include(MyFeatures)

So what is the benefit of using ApplicationRecord, do you think it is necessary to add ApplicationRecord?

BoraMa :

While it may seem the same in basic Rails applications, there actually is an important difference once you begin to use rails engines, plugins / gems or direct methods from ActiveRecord::Base.

  • ActiveRecord::Base.include(MyFeatures) mixes in the features directly into ActiveRecord::Base and it is present there forever for all later uses of ActiveRecord::Base (it cannot be "unmixed") and there is no way to get the original ActiveRecord::Base anymore in any code after the include. This can easily lead to problems if some of the mixed in feature changed the default ActiveRecord behavior or if e.g. two engines / gems tried to include same-named methods.

  • On the other hand, the ApplicationRecord approach makes the features present only for the classes (models) that inherit from it, other classes, as well as direct use of ActiveRecord::Base stay pristine, uncluttered by the module features.

This is especially important when engines or rails plugins are used as it allows them to separate their own model logic from the main application's model logic which was not possible before ApplicationRecord.

All of this is also nicely described in this blog post and this github comment.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rails 5 ApplicationRecord inheritance with exception

Checking if an object is an ApplicationRecord in Rails 5

Ruby on rails 5 uninitialized constant ApplicationRecord (NameError)

Rails 5 query returns :ActiveRecord_Relation instead of data

Rails extending ActiveRecord::Base

Why calling method on interface uses base implementation instead of override?

Rails 5: ActiveRecord OR query

Trying to create an object that doesn't inherit from ActiveRecord/ApplicationRecord within a Rails controller, but failing

Rails 5 joining activerecord scopes with OR

Rails 5 ActiveRecord Delete Duplicates

Disable ActiveRecord in Ruby on Rails 5

Why rails 5 using puma instead of webrick for development purpose?

Rails 5: DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `exists?`. ..I have no idea

Ruby on Rails - Why can't I use ActiveRecord::Base methods? Where to use?

Is ActiveRecord::Base.logger different then rails logger?

Rails - How to override ActiveRecord::Base destroy method?

Why uses TYPO3 smallint(5) instead of tinyint(1) for storing BOOLEAN values in database?

joomla BASE HREF uses HTTP instead of HTTPS

Rails 5: ActiveRecord AND followed by OR condition: A && (B || C)

ActiveRecord::Relation.concat failing in Rails 5

How to create ActiveRecord tableless Model in Rails 5?

Update information in a Hash associated to an ActiveRecord rails 5

Rails 5 rspec - convert object to ActiveRecord Relation

Rails. Why should i use ActiveRecord?

Why Ubuntu uses eglibc instead of glibc?

Why does Typescript uses 'number' instead of int'?

Rails form_for that uses STI base class

Returning a hash instead of array with ActiveRecord::Base.connection

How to get validation constraints of ApplicationRecord model in Rails?