rubocop / rails-style-guide

A community-driven Ruby on Rails style guide
http://rails.rubystyle.guide
6.48k stars 1.06k forks source link

Add delete_all to the list of methods that skip model validations #332

Open etherbob opened 1 year ago

etherbob commented 1 year ago

Is your feature request related to a problem? Please describe.

We were recently burned by a scenario where we were calling delete on some models and/or delete_all on some associations and bypassing various callbacks before_destroys etc.

Describe the solution you'd like

I'm thinking it'd be nice to have delete, and delete_all added to the list for https://rails.rubystyle.guide/#beware-skip-model-validations

Describe alternatives you've considered

I could see a distinct rule of preferring destroy and destroy_all over delete/delete_all being a preferred alternative since really it's about before_destroy callbacks (and/or setting updated_at if you use something like acts_as_paranoid) vs hitting the actual model validations

Additional context

Sorry if this is a little thin. Given a bit more time (and assuming it's wanted) I'll take a swing at the alternative option and submit it as a PR.

andyw8 commented 1 year ago

I agree, both should be added.

koic commented 1 year ago

Beware of the behavior of the following methods.

Can you try to open a PR to the Rails Guides, which is the link to the above Rails Style Guide, first? https://guides.rubyonrails.org/active_record_validations.html#skipping-validations

Once added to that, it could then be added to the Style Guide. Eventually it will be reflected in the style guide implementation Rails/SkipsModelValidations.