Using find_each to avoid memory problems

When selecting records with ActiveRecord in Rails all of the selected objects are usually loaded into memory, if you are dealing with a large record set (for example in a background job, rake task or migration) this can lead to memory problems that result in your code crashing.

By using find_each at the end your scope you will only a small set of records into memory at a time:

Note.all.find_each { |note| note.update(body: note.body.reverse) }

For more information see the documentation here.