You need to fix some data so you’ll just iterate through it all and fix it, right?
User.has_purchased(true).each do |customer|
customer.grant_role(:customer)
end
You have an ecommerce site with a million customers. Let’s say each User object takes 500 bytes. This code will take 500MB of memory at runtime! Better:
User.has_purchased(true).find_each do |customer|
customer.grant_role(:customer)
end
find_each uses find_in_batches to pull in 1000 records at a time, dramatically lowering the runtime memory requirements.