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

Possible false positives with Rails/FindEach #488

Open
tagliala opened this issue Feb 3, 2024 · 0 comments
Open

Possible false positives with Rails/FindEach #488

tagliala opened this issue Feb 3, 2024 · 0 comments

Comments

@tagliala
Copy link
Contributor

tagliala commented Feb 3, 2024

Hello,

sorry this is not actually an issue, but may help someone to avoid mistakes.

It is possible that most of find_each offenses found in views are false positives and fixing them will lead to errors, because find_each overrides the order, which is most likely set on the controller or somewhere else, maybe even in a default scope

Example:

%ul
  - @cities.includes(:country).each do |city|
    %li #{city} (#{city.country})
$ haml-lint
test.haml:2 [W] RuboCop: Rails/FindEach: Use `find_each` instead of `each`.

NOTES:

  • @cities.each does not report an issue, the problem is with AR clauses like where and includes
  • It may be not a best practice to use includes or where in views, but that would be a different issue

In this context, cities were probably been sorted in the controller or somewhere else. find_each will revert their order

For the moment I'm using

Rails/FindEach:
  Exclude:
    - '**/*.haml'

Maybe these Rails/* cops causing troubles should be documented somewhere, together with #443

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants