Skip to content

Instantly share code, notes, and snippets.

View mmclead's full-sized avatar

Mason McLead mmclead

  • Software.com
  • San Diego, CA
View GitHub Profile
@mmclead
mmclead / Insert lots of records
Created June 20, 2012 20:53
Create lots of records faster with raw SQL in RoR and PostgreSQL
#### Mass SQL INSERT Method ####
hash = {{a=>1,b=>2},{a=>3,b=>4},{a=>5,b=>6}} #data to insert
connection = ActiveRecord::Base.connection
if connection.adapter_name.downcase == "sqlite" #Default way to do things
parent.transaction do
hash.each do |h|
parent.children.create!(h)
end
end
elsif connection.adapter_name.downcase == "postgresql" #Faster way to do things