Module: Earth::Model::SafeFinders

Defined in:
lib/earth/model.rb

Instance Method Summary collapse

Instance Method Details

#safe_find_eachObject



83
84
85
86
87
# File 'lib/earth/model.rb', line 83

def safe_find_each
  safe_find_in_batches do |records|
    records.each { |record| yield record }
  end
end

#safe_find_in_batches(&block) ⇒ Object

www.seejohncode.com/tag/rails/ Override due to implementation of regular find_in_batches conflicting using UUIDs



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/earth/model.rb', line 69

def safe_find_in_batches(&block)
  return find_in_batches({}, &block) if relation.primary_key.is_a?(Arel::Attributes::Integer)

  batch_size = 1000
  offset = 0
  # Get the relation and keep going over it until there's nothing left
  relation = order("#{quoted_table_name}.#{quoted_primary_key} ASC").limit(batch_size)
  while (results = relation.offset(offset).limit(batch_size).all).any?
    unscoped { block.call(results) }
    offset += batch_size
  end
  nil
end