each
AnyCollectionClass.each do |element|
element.any_method
end
a shortcut, same as above
AnyCollectionClass.each(&:any_method)
class A
def x
puts "call x method"
end
end
[A.new, A.new, A.new].each(&:x)
# => call x method
# => call x method
# => call x method
each vs find_each
An active record relation does not automatically load all records into memory.
When you call
#each
, all records will be loaded into memory. When you call#find_each
, records will be loaded into memory in batches of the given batch size.So when your query returns a number of records that would be too much memory for the server's available resources, then using
#find_each
would be a great choice.