简体   繁体   中英

Rubocop command to disable Rails/SkipsModelValidations: avoid using update_all error

I want to change one column to is_deleted: true in my records by below line:

UserTag.where(cms_tag_id: webhook.dig('entity', 'id')).update_all(is_deleted: true)

But I'm getting Rubocop's error:

Rails/skipsmodelvalidations: avoid using update_all because it skips validations.

I know I can use each block and update all columns through e.update in a block but update_all is much faster, one line, neat solution.

How to disable such a rubocop error only for this, specific line?

You can run the below command, which will generate a.rubocop_todo.yml file which will record and ignore specific offences for offending files:

rubocop --auto-gen-config --exclude-limit 999 --no-offense-counts

--auto-gen-config generates the yml file, the only non-optional flag to achieve what you are looking for.

--exclude-limit xxx is a number of offence before rubocop disables the check for the entire application.

--no-offense-counts does not record a count how many offences there are in the yml file.

Ensure you've thought about the consequences of ignoring linters; it is usually ill advised to not follow the linter advice. Be aware that this will disable checks for all offences in the spec tests.

Update

If you want to disable rubocop checks without generating a file, you can use a comment like so:

Company.where(cms_tag_id: webhook.dig('entity', 'id')).update_all(is_deleted: true) # rubocop:disable Rails/SkipsModelValidations

To expand on @benjessop's answer:
To disable RuboCop selectively for a block of code, use # rubocop:disable ... # rubocop:enable block, like so:

# rubocop:disable Rails::SkipsModelValidations

# The specified cop is not enforced here.
# Therefore, make this block of code minimal. 
# For example, just this one line:

UserTag.where(cms_tag_id: webhook.dig('entity', 'id')).update_all(is_deleted: true)

# rubocop:enable Rails::SkipsModelValidations

SEE ALSO:

Disabling Cops within Source Code
More general info on RuboCop Configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM