Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: iterating over all objects of a type without loading them all at once

Tags:

class Car << ActiveRecord::Base end  Car.all.each do |car|   # do stuff end 

This loads all the objects of type Car into memory (I think) and iterates through them. What I want instead is to iterate through all the ids and load them one at a time like this:

Car.all_ids.each do |id|   c = Car.find id   # do stuff end 

But all_ids doesn't exist, is there an equivalent?

like image 843
SteveRawlinson Avatar asked Apr 27 '11 16:04

SteveRawlinson


2 Answers

For Rails 2.3 and up (including Rails 3) the easiest solution is find_each:

Car.find_each do |car|   # do stuff end 

This executes the query in batches for you automatically. The default batch size is 1000 but you can set your own. It also works alongside named scopes and ActiveRecord::Relations:

Car.hotrods.where(:color => 'red').find_each(:batch_size => 10) { do |car| ... } 

See http://guides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects-in-batches

like image 83
naudster Avatar answered Sep 20 '22 15:09

naudster


You could use find_in_batches, which fetches the records x at a time, where x is configurable and by default 1000.

  Person.where("age > 21").find_in_batches do |group|     # group is an array of 1000 records     group.each { |person| person.party_all_night! }   end 
like image 32
ryeguy Avatar answered Sep 20 '22 15:09

ryeguy