-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
Fix an error for Rails/UniqueValidationWithoutIndex
#1028
Conversation
validates :account, presence: true | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Uniqueness validation should have a unique index on the database column. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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? |
418a64f
to
b97ee12
Compare
Resolves rubocop#1022 (comment). This PR fixes an error for `Rails/UniqueValidationWithoutIndex` when the `presence: true` option is used alone for the `validates` method.
b97ee12
to
26dce91
Compare
Tested this PR against our codebase, and it fixed the problem for us. Thank you! |
Resolves #1022 (comment).
This PR fixes an error for
Rails/UniqueValidationWithoutIndex
when thepresence: true
option is used alone for thevalidates
method.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.