December 9th, 2008
Have a big migration that would take too long to complete? It would be too bad to inadvertently bring down your whole server by loading a big dataset into memory.
A very simple solution is to use will_paginate. I was just combing through the plugin code, and found out that will_paginate has a nice method called
paginated_each, which “Iterates through all records by loading one page at a time. This is useful for migrations or any other use case where you don’t want to load all the records in memory at once.”
So instead of writing
for article in Article.find(all), you’re better off using
Article.paginated_each do |article|. Your migrations will finish faster and with less memory usage.