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

前端 未结 2 1099
抹茶落季
抹茶落季 2020-12-13 03:35

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

But basically, I think every technic

2条回答
  •  -上瘾入骨i
    2020-12-13 04:18

    This is to expand on @BoraMa's answer, and to, hopefully, clear up some confusion around ActiveRecord::Base.abstract_class.

    ActiveRecord::Base.abstract_class goes back to at least Rails 3.2.0 (http://api.rubyonrails.org/v3.2.0/classes/ActiveRecord/Inheritance/ClassMethods.html), which was released on January 20, 2012.

    Rails 4.0.0 improved the documentation: http://api.rubyonrails.org/v4.0.0/classes/ActiveRecord/Inheritance/ClassMethods.html

    So, to everyone who thinks ApplicationRecord is radically new, it's not. It is an improvement, not a breaking change. Nothing was added to ActiveRecord::Base to make this work.

    I did the same thing on a Rails 4.2.6 project because the models used UUIDs for ids instead of integers, and this required a change to the default ORDER BY. So, instead of using copy-paste or a concern, I went with inheritance using a UuidModel class and self.abstract_class = true.

提交回复
热议问题