Use find_each to iterate over a collection of AR objects

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: rails-best-practices/find-each

Language: Ruby

Severity: Notice

Category: Best Practices

Description

This rule ensures efficient use of memory when dealing with large collections of ActiveRecord (AR) objects. The each method loads all the objects at once into memory, which can cause significant performance issues if the collection is large. In contrast, find_each loads a batch of records (1000 by default) into memory, processes them, and then loads the next batch, significantly reducing memory usage.

This rule is especially relevant when dealing with large datasets, where the each method can lead to ‘out of memory’ errors.

To adhere to this rule, replace each with find_each when iterating over collections of ActiveRecord objects. For example, instead of writing Foo.all.each, write Foo.all.find_each. Similarly, replace Foo.where('foo > 42').each with Foo.where('foo > 42').find_each.

Non-Compliant Code Examples

Foo.all.each do |foo_instance|
  foo_instance.do_awesome_stuff
end

Foo.where('foo > 42').each do |foo_instance|
  foo_instance.party_all_night!
end

Compliant Code Examples

Foo.all.find_each do |foo_instance|
  foo_instance.do_awesome_stuff
end

Foo.where('foo > 42').find_each do |foo_instance|
  foo_instance.party_all_night!
end
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis