Mysterious Trousers

Quality software and amazing support since 2010

We’re a small team dedicated to making you happy. We achieve this through synergizing analytics and best-of-breed social blah blah blah hard work and honest-to-goodness customer service.

find_each, not all

This great presentation at RailsConf 2012 by James Edward Grey II pointed out something I hadn’t taken into account. This:

User.all do |user| # do something... end

is bad mojo for potential large tables because it will attempt to load the entire table into memory. A better habit to get into is:

User.find_each do |user| # do something... end

This will have the same net result, but it will only pull 1000 records at a time into memory.

© 2013 Mysterious Trousers, LLC. All Rights Reserved. Background photo by hsld