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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With