Skip to content

Instantly share code, notes, and snippets.

View the-architect's full-sized avatar
🐒
working on ... 👨🏼‍💻

Marcel Scherf the-architect

🐒
working on ... 👨🏼‍💻
View GitHub Profile
class ActiveRecord::Base
def self.import!(record_list)
raise ArgumentError 'record_list not an Array of Hashes' unless record_list.is_a?(Array) && record_list.all? {|rec| rec.is_a? Hash }
return if record_list.empty?
record_list.in_groups_of(1000, false).each do |record_group|
key_list, value_list = convert_record_list(record_group)
sql = "INSERT INTO #{self.table_name} (#{key_list.join(', ')}) VALUES #{value_list.map {|rec| "(#{rec.join(', ')})" }.join(' ,')}"
self.connection.insert_sql(sql)