Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 652
mr_muscle Avatar asked Apr 27 '26 03:04

mr_muscle


2 Answers

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

like image 199
Timur Shtatland Avatar answered Apr 30 '26 00:04

Timur Shtatland


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
like image 29
benjessop Avatar answered Apr 30 '26 00:04

benjessop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!