.all.each is highly inefficient with many rows

According to http://guides.rubyonrails.org/active_record_querying.html, chapter 1.3, you should never use .all.each when iterating over the whole table.