Skip to content

Commit 1ca074e

Browse files
authored
Merge pull request #573 from koic/fix_an_error_for_rails_find_each
Fix an error for `Rails/FindEach`
2 parents 284f3e1 + d470952 commit 1ca074e

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#573](https://github.com/rubocop/rubocop-rails/pull/573): Fix an error for `Rails/FindEach` when using `where` with no receiver. ([@koic][])

lib/rubocop/cop/rails/find_each.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def active_model_error_where?(node)
5555
end
5656

5757
def active_model_error?(node)
58+
return false if node.nil?
59+
5860
node.send_type? && node.method?(:errors)
5961
end
6062
end

spec/rubocop/cop/rails/find_each_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,18 @@
4545
# Active Model Errors slice from the new query interface introduced in Rails 6.1.
4646
it 'does not register an offense when using `model.errors.where`' do
4747
expect_no_offenses(<<~RUBY)
48-
model.errors.where(:title).each { |error| do_something(error) }
48+
class Model < ApplicationRecord
49+
model.errors.where(:title).each { |error| do_something(error) }
50+
end
51+
RUBY
52+
end
53+
54+
it 'registers an offense when using `where` with no receiver' do
55+
expect_offense(<<~RUBY)
56+
class Model < ApplicationRecord
57+
where(record: [record1, record2]).each(&:touch)
58+
^^^^ Use `find_each` instead of `each`.
59+
end
4960
RUBY
5061
end
5162

0 commit comments

Comments
 (0)