Why ApplicationRecord

Why create an ApplicationRecord class inheriting from ActiveRecord::Base and then have all models inherit from ApplicationRecord instead of ActiveRecord::Base ?

You can find rationale from original PR which introduced this feature https://github.com/rails/rails/pull/22567

I know a guy that doesn’t use rails generate controller or rails generate model… So when he makes a controller he just handcodes it.

For him I suppose it’d be an advantage, as

It is easier to write

class Abc < ApplicationController

end

than to write

class Abc < ActionController::Base

protect_from_forgery with: :exception

end

class Abc should say class AbcController

>Why create an ApplicationRecord class inheriting from ActiveRecord::Base and then have all models inherit from ApplicationRecord instead of ActiveRecord::Base ?

I know a guy that doesn't use rails generate controller or rails generate model.. So when he makes a controller he just handcodes it.

For him I suppose it'd be an advantage, as

It is easier to write

class Abc < ApplicationController
end

than to write

class Abc < ActionController::Base
  protect_from_forgery with: :exception
end

It's meant to be a way to gather common features into one place. If all your routes are meant to have CanCanCan protecting them from unauthorized access, then you may see this in the ApplicationController:

  unless: :devise_controller?
    before_action :authenticate_user!
    load_and_authorize_resource
  end

Which means that you won't forget to add that to your controllers and leave something important exposed to the public. There's lots of other useful stuff you can wedge in the ApplicationController that you need in all controllers, and thus only have to write once. Inheritance can be useful!

Walter