Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # File activerecord/lib/active_record/batches.rb, line 55
- def find_in_batches(options = {})
- raise "You can't specify an order, it's forced to be #{batch_order}" if options[:order]
- raise "You can't specify a limit, it's forced to be the batch_size" if options[:limit]
- start = options.delete(:start).to_i
- batch_size = options.delete(:batch_size) || 1000
- proxy = scoped(options.merge(:order => batch_order, :limit => batch_size))
- records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} >= ?", start ])
- while records.any?
- yield records
- break if records.size < batch_size
- last_value = records.last.id
- raise "You must include the primary key if you define a select" unless last_value.present?
- records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} > ?", last_value ])
- end
- end
Add Comment
Please, Sign In to add comment