Guest User

Untitled

a guest
Jan 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # File activerecord/lib/active_record/batches.rb, line 55
  2. def find_in_batches(options = {})
  3. raise "You can't specify an order, it's forced to be #{batch_order}" if options[:order]
  4. raise "You can't specify a limit, it's forced to be the batch_size" if options[:limit]
  5.  
  6. start = options.delete(:start).to_i
  7. batch_size = options.delete(:batch_size) || 1000
  8.  
  9. proxy = scoped(options.merge(:order => batch_order, :limit => batch_size))
  10. records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} >= ?", start ])
  11.  
  12. while records.any?
  13. yield records
  14.  
  15. break if records.size < batch_size
  16.  
  17. last_value = records.last.id
  18.  
  19. raise "You must include the primary key if you define a select" unless last_value.present?
  20.  
  21. records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} > ?", last_value ])
  22. end
  23. end
Add Comment
Please, Sign In to add comment