What am I doing wrong with find_in_batches?

I’m testing out ActiveRecord’s find_in_batches and I’m missing
something very basic I think. I can only get it to return the values
from the first chunk (batch_size) and nothing more…how do I get it
to process chunks until there are no more?

From my Rails 2.3 console, shouldn’t this find_in_batches method print
the same number as the standard count method? I mean, shouldn’t these
be equivalent?

Entry.count(:select => “distinct(email)”, :conditions => { :opt_in =>
true, :blacklist => false })


count = 0
Entry.find_in_batches(:batch_size => 2000, :select =>
“distinct(email)”, :conditions => { :opt_in => true, :blacklist =>
false} ) do |entries|
entries.each { count += 1 }
puts count



Thanks for any help!

Looks like you’re missing the “id” in your :select, which
#find_in_batches relies upon. Try:

:select => “distinct(email), id”

Perfect! Thanks Erol. I knew it was something very basic.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs