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

Cop Suggestion: warn against usage of update_attribute in Rails #3743

Closed
rahulcs opened this issue Nov 29, 2016 · 2 comments
Closed

Cop Suggestion: warn against usage of update_attribute in Rails #3743

rahulcs opened this issue Nov 29, 2016 · 2 comments

Comments

@rahulcs
Copy link
Contributor

rahulcs commented Nov 29, 2016

The update_attribute method skips validations and will save the object to database regardless of its validity.
These methods should be used with caution.

Prefer this:

user.update_attributes(email: '[email protected]')

over:

user.update_attribute(email: '[email protected]')

Bugs resulting from this can be hard to identify, and I cannot see a valid use-case for updating a model attribute without validation.

Is this a reasonable candidate for inclusion as a Rails-specific cop in RuboCop?

@tejasbubane
Copy link
Contributor

Not just update_attribute, I think we should we warn against all of those methods which skip validations. Even save(validate: false).

Is this a reasonable candidate for inclusion as a Rails-specific cop in RuboCop?

Sounds like a pretty reasonable candidate 👍 Let's see what others have to say.

@rrosenblum
Copy link
Contributor

Not just update_attribute, I think we should we warn against all of those methods which skip validations. Even save(validate: false).

I agree that we should warn against all of the methods that skip validations. I don't think that we should warn against save(validate: false) because someone has to explicitly say that they do not want validations. The other ActiveRecord methods are most likely used by mistake.

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

No branches or pull requests

3 participants