Method: MassRecord::Actions#mass_save_by_table

Defined in:
lib/mass_record.rb

#mass_save_by_table(json_objects, key: {}) ⇒ Object



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/mass_record.rb', line 528

def mass_save_by_table json_objects, key:{}
	begin
		tables = json_objects.collect{|x| x[key[:table]]}.to_set.to_a

		errors = {}
		tables.each do |table|
			# logger.info "Table: #{table}".black.on_white
			# sort the hashes by operation type
			sorted_hashes = sort_save_operations from:json_objects, for_table:table, key:key

			# perform the appropriate operations
			model = get_model from:table
			errors[table.to_sym] = {}
			errors[table.to_sym].merge! mass_update sorted_hashes[:update], into:model unless sorted_hashes[:update].blank?
			errors[table.to_sym].merge! mass_insert sorted_hashes[:insert], into:model unless sorted_hashes[:insert].blank?
		end
		return errors
	rescue Exception => e
		return {run_time:e} unless defined? errors
		errors[:run_time] = e if defined? errors
		return errors
	end
end