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

Fix an error for Rails/UniqueValidationWithoutIndex #1028

Conversation

koic
Copy link
Member

@koic koic commented Jun 19, 2023

Resolves #1022 (comment).

This PR fixes an error for Rails/UniqueValidationWithoutIndex when the presence: true option is used alone for the validates method.


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.

Comment on lines 79 to 80
validates :account, presence: true
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Uniqueness validation should have a unique index on the database column.
Copy link

@rnestler rnestler Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this really register an offense in this case? Doesn't make sense in my opinion, since there is no uniqueness requirement on the field?

Copy link
Contributor

@mobilutz mobilutz Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think this should not raise an error.

Think about this example:

class Post
  belongs_to :user

  validates :user, presence: true
end

class User
  has_many :posts
end

The column user_id in posts can NEVER be unique because a user can have many posts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the comments. This is a bug and I'm working now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this PR.

@ydah
Copy link
Member

ydah commented Jun 19, 2023

Ah, #1021 correction was not good enough. Maybe, it should have been as follows.

- return if uniqueness_part(node)&.falsey_literal?
+ return unless uniqueness_part =  uniqueness_part(node)
+ return if uniqueness_part.falsey_literal?

@koic koic force-pushed the fix_an_error_for_rails_unique_validation_without_index branch 4 times, most recently from 418a64f to b97ee12 Compare June 19, 2023 10:02
Resolves rubocop#1022 (comment).

This PR fixes an error for `Rails/UniqueValidationWithoutIndex`
when the `presence: true` option is used alone for the `validates` method.
@koic koic force-pushed the fix_an_error_for_rails_unique_validation_without_index branch from b97ee12 to 26dce91 Compare June 19, 2023 10:03
@markijbema
Copy link

Tested this PR against our codebase, and it fixed the problem for us. Thank you!

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.

The Rails/UniqueValidationWithoutIndex cop is returning undefined method error
5 participants