What am I doing wrong with find_in_batches?

5 views
Skip to first unread message

Lee Smith

unread,
Oct 12, 2010, 12:26:02 AM10/12/10
to Ruby on Rails: Talk
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 })

313954

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
end

2000

http://pastie.org/1214575

Thanks for any help!

Erol Fornoles

unread,
Oct 12, 2010, 12:43:49 AM10/12/10
to Ruby on Rails: Talk
Looks like you're missing the "id" in your :select, which
#find_in_batches relies upon. Try:

:select => "distinct(email), id"

Lee Smith

unread,
Oct 12, 2010, 9:33:30 AM10/12/10
to Ruby on Rails: Talk
Perfect! Thanks Erol. I knew it was something very basic.
Reply all
Reply to author
Forward
0 new messages