rubocop 0.49.1 shouldn't suggest ApplicationRecord for models in a Rails 4 app

Created on 4 Sep 2017  Â·  4Comments  Â·  Source: rubocop-hq/rubocop

Expected behavior

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

Actual behavior

Offenses:

app/models/activity.rb:21:18: C: Rails/ApplicationRecord: Models should subclass ApplicationRecord.
class Activity < ActiveRecord::Base
                 ^^^^^^^^^^^^^^^^^^

Steps to reproduce the problem

› rubocop -R app/models/activity.rb

Rails version

› rails -v
Rails 4.1.16

RuboCop version

› rubocop -V
0.49.1 (using Parser 2.4.0.0, running on ruby 2.3.1 x86_64-darwin13)

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.

AllCops:
  TargetRailsVersion: 4.0

All 4 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kirrmann picture kirrmann  Â·  3Comments

deivid-rodriguez picture deivid-rodriguez  Â·  3Comments

tedPen picture tedPen  Â·  3Comments

Aqualon picture Aqualon  Â·  3Comments

lepieru picture lepieru  Â·  3Comments