diff --git a/changelog/fix_an_error_for_rails_unique_validation_without_index.md b/changelog/fix_an_error_for_rails_unique_validation_without_index.md new file mode 100644 index 0000000000..0f22367836 --- /dev/null +++ b/changelog/fix_an_error_for_rails_unique_validation_without_index.md @@ -0,0 +1 @@ +* [#1021](https://github.com/rubocop/rubocop-rails/pull/1021): Fix an error for `Rails/UniqueValidationWithoutIndex`. ([@ydah][]) diff --git a/lib/rubocop/cop/rails/unique_validation_without_index.rb b/lib/rubocop/cop/rails/unique_validation_without_index.rb index 7aa0a73381..f8ba542883 100644 --- a/lib/rubocop/cop/rails/unique_validation_without_index.rb +++ b/lib/rubocop/cop/rails/unique_validation_without_index.rb @@ -31,7 +31,7 @@ class UniqueValidationWithoutIndex < Base RESTRICT_ON_SEND = %i[validates].freeze def on_send(node) - return if uniqueness_part(node).falsey_literal? + return if uniqueness_part(node)&.falsey_literal? return if condition_part?(node) return unless schema diff --git a/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb b/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb index 682c93c8d5..ae280df549 100644 --- a/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb +++ b/spec/rubocop/cop/rails/unique_validation_without_index_spec.rb @@ -2,12 +2,24 @@ RSpec.describe RuboCop::Cop::Rails::UniqueValidationWithoutIndex, :config do context 'without db/schema.rb' do - it 'does nothing' do - expect_no_offenses(<<~RUBY) - class User < ApplicationRecord - validates :account, uniqueness: true - end - RUBY + context 'when using `uniqueness: true`' do + it 'does nothing' do + expect_no_offenses(<<~RUBY) + class User < ApplicationRecord + validates :account, uniqueness: true + end + RUBY + end + end + + context 'when using other validation helpers' do + it 'does nothing' do + expect_no_offenses(<<~RUBY) + class User < ApplicationRecord + validates :name, presence: true + end + RUBY + end end end