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

Rails/FindEach not safe-auto-correct #694

Closed
altherlex opened this issue Apr 22, 2022 · 5 comments · Fixed by #972
Closed

Rails/FindEach not safe-auto-correct #694

altherlex opened this issue Apr 22, 2022 · 5 comments · Fixed by #972
Labels
bug Something isn't working

Comments

@altherlex
Copy link

My class is not ActiveRecord but it has a method called all

Rubocop-rails changes from .all.each to all.find_each

@andyw8
Copy link
Contributor

andyw8 commented Apr 22, 2022

Which version of the gem are you using? The spec indicates it should ignore non-AR classes.

it 'does not register an offense when not inheriting `ApplicationRecord`' do
expect_no_offenses(<<~RUBY)
class C < Foo
all.each { |u| u.x }
end
RUBY
end

@altherlex
Copy link
Author

rubocop (1.26.1)
rubocop-rails (2.14.2)

@altherlex
Copy link
Author

altherlex commented Apr 22, 2022

it is a module thou

module Symptoms
  def self.all
    data
  end
end

Symptoms.all.each

@palkan
Copy link
Contributor

palkan commented Jan 20, 2023

Surprisingly, it's not safe even for Active Record:

users = User.where(admin: true).each(&:itself) #=> [<User...>, ...]

users = User.where(admin: true).find_each(&:itself) #=> nil

@TastyPi
Copy link

TastyPi commented Jan 27, 2023

I've just hit this using 2.17.4, it's trying to convert each to find_each when the object is an Array. The class the call is in is not an ActiveRecord, though it is defined under app/models.

It is using ActiveModel::Model, might that be the issue? I can try to make a minimal reproduction if that helps.

koic added a commit to koic/rubocop-rails that referenced this issue Apr 3, 2023
Fixes rubocop#694.

This PR marks `Rails/FindEach` as unsafe.
koic added a commit to koic/rubocop-rails that referenced this issue Apr 3, 2023
Fixes rubocop#694 and rubocop#397.

This PR marks `Rails/FindEach` as unsafe.
@koic koic added the bug Something isn't working label Apr 4, 2023
@koic koic closed this as completed in #972 Apr 4, 2023
koic added a commit that referenced this issue Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants