Skip to content

Commit 3ba2b30

Browse files
authored
Merge pull request #218 from koic/fix_error_for_unique_validation_without_index
[Fix #214] Fix an error for `Rails/UniqueValidationWithoutIndex`
2 parents 789a8f2 + fdeeba5 commit 3ba2b30

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
* [#213](https://github.com/rubocop-hq/rubocop-rails/pull/213): Fix a false positive for `Rails/UniqueValidationWithoutIndex` when using conditions. ([@sunny][])
88
* [#215](https://github.com/rubocop-hq/rubocop-rails/issues/215): Fix a false positive for `Rails/UniqueValidationWithoutIndex` when using Expression Indexes. ([@koic][])
9+
* [#214](https://github.com/rubocop-hq/rubocop-rails/issues/214): Fix an error for `Rails/UniqueValidationWithoutIndex`when a table has no column definition. ([@koic][])
910

1011
## 2.5.0 (2020-03-24)
1112

lib/rubocop/rails/schema_loader/schema.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def with_column?(name:)
6060

6161
def build_columns(node)
6262
each_content(node).map do |child|
63-
next unless child.send_type?
63+
next unless child&.send_type?
6464
next if child.method?(:index)
6565

6666
Column.new(child)
@@ -69,7 +69,7 @@ def build_columns(node)
6969

7070
def build_indices(node)
7171
each_content(node).map do |child|
72-
next unless child.send_type?
72+
next unless child&.send_type?
7373
next unless child.method?(:index)
7474

7575
Index.new(child)
@@ -79,7 +79,7 @@ def build_indices(node)
7979
def each_content(node)
8080
return enum_for(__method__, node) unless block_given?
8181

82-
case node.body.type
82+
case node.body&.type
8383
when :begin
8484
node.body.children.each do |child|
8585
yield(child)

spec/rubocop/cop/rails/unique_validation_without_index_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,23 @@ class Article
303303
end
304304
end
305305

306+
context 'when a table has no column definition' do
307+
let(:schema) { <<~RUBY }
308+
ActiveRecord::Schema.define(version: 2020_02_02_075409) do
309+
create_table "users", force: :cascade do |t|
310+
end
311+
end
312+
RUBY
313+
314+
it 'ignores it' do
315+
expect_no_offenses(<<~RUBY)
316+
class User
317+
validates :account, uniqueness: true
318+
end
319+
RUBY
320+
end
321+
end
322+
306323
context 'when the validation is for a relation with foreign_key: option' do
307324
context 'without proper index' do
308325
let(:schema) { <<~RUBY }

0 commit comments

Comments
 (0)