tl;dr: Use with_index
find_each
with index If you do not provide a block to find_each, it will return an Enumerator Show archive.org snapshot for chaining with other methods:
Person.find_each.with_index do |person, index|
person.award_trophy(index + 1)
end
map
with index Similarly, you may need an index when using other methods, like map
, flat_map
, detect
(when you need the index for detection), or similar. Here is an example for map
:
people.map.with_index do |person, index|
person.at_rank(index + 1)
end