rubocop / rubocop-rails

A RuboCop extension focused on enforcing Rails best practices and coding conventions.
https://docs.rubocop.org/rubocop-rails
MIT License
819 stars 263 forks source link

Suggest find_each instead of iterating in a find_in_batches block #417

Open Darhazer opened 3 years ago

Darhazer commented 3 years ago

Is your feature request related to a problem? Please describe.

I've seen code like

Model.find_in_batches.each do |batch|
  batch.each do |item|
    ...
  end
end

This could be safely replaced by find_each. Note - the .each at the end doesn't make a difference, it could be just find_in_batches do. As long as find_in_batches has no options, and the iteration is the only thing in the block, it could be find_each instead.

Describe the solution you'd like

Whether this should be supported by FindEach or a new cop, I have no opinion.

Describe alternatives you've considered

I guess this could be easily added to the existing FindEach cop, though it would add to the complexity of the otherwise quite simple cop.

koic commented 3 years ago

Whether this should be supported by FindEach or a new cop, I have no opinion.

The existing Rails/FindEach cop is focused on performance. On the other hand, I think the issue is focused on lint or style. So I think it's better to have a different cop than Rails/FindEach cop.