Skip to content

Instantly share code, notes, and snippets.

@amichal
amichal / bulk_merge.rb
Created August 2, 2012 18:12
csv export and bulk load any rails scope
# Bulk merge a bunch of records
#
# we expect most of the records to import to represent updates
# so this method tries to be efficent with database queries
# by using a single query to find any existing records matching the key_attributes
# updating those records and finally inserting each remaining record
#
# It DOEST NOT use a transaction or lock. callers responsibilty to
# add that if desired
#
@amichal
amichal / merge.rb
Created January 13, 2012 22:17
active record bulk merge
class ActiveRecord::Base
# Bulk merge a bunch of records
#
# we expect most of the records to import to represent updates
# so this method tries to be efficent with database queries
# by using a single query to find any existing records matching the key_attributes
# updating those records and finally inserting each remaining record
#
# It DOEST NOT use a transaction or lock. callers responsibilty to
# add that if desired