Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Rails/RedundantActiveRecordAllMethod to ignore delete_all and destroy_all when receiver is an association #1171

Merged
merged 1 commit into from
Nov 16, 2023

Conversation

masato-bkn
Copy link
Contributor

@masato-bkn masato-bkn commented Oct 29, 2023

This PR changes Rails/RedundantActiveRecordAllMethod to ignore delete_all and destroy_all when the receiver is an association (e.g., user.articles.all.delete_all). It only checks cases where the receiver is a model.

As pointed out in rubocop/rails-style-guide#347, it has been found that when the receiver of all is an association, omitting all can change the behavior of certain methods. I think that in cases where behavior could change, it would be better for the cop not to add an offense.

Methods that change behavior

Currently, among the methods targeted by this cop, the following methods have been confirmed to change behavior when all is omitted from an association:

  • delete_all
  • destroy_all

delete_all

user.articles.all.delete_all
# Method: `ActiveRecord::Relation#delete_all`
# SQL: DELETE FROM articles WHERE articles.user_id = 1
# Doc:  https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete_all

user.articles.delete_all
# Method: `ActiveRecord::Associations::CollectionProxy#delete_all`  
# SQL: UPDATE articles SET user_id = NULL WHERE articles.user_id = 1
# Doc: https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete_all

destroy_all

user.articles.all.destroy_all 
# Method: `ActiveRecord::Relation#destroy_all`
# Behavior: Does not execute callbacks
# Doc: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy_all

user.articles.destroy_all
# Method: `ActiveRecord::Associations::CollectionProxy#destroy_all`
# Behavior: Executes callbacks
# Doc: https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-destroy_all

Additional Context

Several approaches can be taken regarding the cop itself, but it's challenging to decide on a policy:

  1. Add an offense for delete_all and destroy_all only when the receiver is a Model (e.g., User.all.delete_all). If the receiver might be an association, do not add an offense. (<- This PR)
  2. For cases where the receiver of all is an association for delete_all and destroy_all, output an offensive message indicating that omitting all changes the behavior.
    • It might be challenging to come up with a concise and clear message.
  3. Do not modify the cop implementation. Only add a cautionary note to the this cop description.

I think that in cases where behavior could change, it would be better for the cop not to add an offense. I had my doubts, but for this PR, I've decided to adopt the first approach.

Any feedback on the policy for handling this cop would be greatly appreciated.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
    * [ ] If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@masato-bkn masato-bkn force-pushed the restrict-offense-to-model-receiver branch 2 times, most recently from 8492e5d to 077cac5 Compare October 29, 2023 01:19
@masato-bkn masato-bkn marked this pull request as ready for review October 29, 2023 01:22
@masato-bkn masato-bkn force-pushed the restrict-offense-to-model-receiver branch from 077cac5 to 380e9c8 Compare October 30, 2023 15:34
…and `destroy_all` when receiver is an association
@masato-bkn masato-bkn force-pushed the restrict-offense-to-model-receiver branch from 380e9c8 to 7f5514e Compare October 30, 2023 23:45
@koic koic merged commit c176f6b into rubocop:master Nov 16, 2023
10 checks passed
@koic
Copy link
Member

koic commented Nov 16, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants