Rubocop shouldn't trigger and suggest using ApplicationRecord for models in an app using Rails 4, since ApplicationRecord was first introduced in Rails 5.
Rails/ApplicationRecord: Models should subclass ApplicationRecord
Offenses:
app/models/activity.rb:21:18: C: Rails/ApplicationRecord: Models should subclass ApplicationRecord.
class Activity < ActiveRecord::Base
^^^^^^^^^^^^^^^^^^
› rubocop -R app/models/activity.rb
› rails -v
Rails 4.1.16
› rubocop -V
0.49.1 (using Parser 2.4.0.0, running on ruby 2.3.1 x86_64-darwin13)
HI.
This cop supports TargetRailsVersion option.
https://github.com/bbatsov/rubocop/blob/e21d0ed0e39d30a495a3e44a20a086514606ceda/lib/rubocop/cop/rails/application_record.rb#L22
And default value of the option is 5.0.
https://github.com/bbatsov/rubocop/blob/e21d0ed0e39d30a495a3e44a20a086514606ceda/config/default.yml#L118
So, if your project using Rails 4, please set the option.
AllCops:
TargetRailsVersion: 4.0
Thanks. I wasn't aware of that option, but added it to our .rubocop.ymlfile now.
So we ought to use TargetRailsVersion: 4.0 for all Rails versions 4.x?
So we ought to use TargetRailsVersion: 4.0 for all Rails versions 4.x?
No, we can use TargetRailsVersion: 4.1 for Rails 4.1. But currently, RuboCop does not have a cop that is aware of minor version. Existing cops are aware of major version only.
At present, specifying minor version does not affect, but I think specifying minor version is better for the future.
Ok thanks again, it's nice to know. :) Have a good day!
Most helpful comment
HI.
This cop supports TargetRailsVersion option.
https://github.com/bbatsov/rubocop/blob/e21d0ed0e39d30a495a3e44a20a086514606ceda/lib/rubocop/cop/rails/application_record.rb#L22
And default value of the option is
5.0.https://github.com/bbatsov/rubocop/blob/e21d0ed0e39d30a495a3e44a20a086514606ceda/config/default.yml#L118
So, if your project using Rails 4, please set the option.