Bulk insertion of data with ActiveRecord

Jun 18, 2008

Apologies to all for the extended hiatus, I was in the US for a month on holiday; and then I just got lazy. Anyway, I'm back and plugging someone else's extension. I came across ActiveRecord Extension last night, and was quite frankly dumbfounded that 1) I hadn't come across it before, and 2) More people weren't using it. In short, it will let you use ActiveRecord to batch insert multiple rows in a single query, rather than looping over the Model#create method and getting all the overhead that comes with it (and the hundred insert statements).

Zach Dennis has written a great little plugin called ActiveRecord::Extensions which makes the bulk inserts almost as painless as your regular create.

First, install the gem:

sudo gem install ar-extensions

Include it in your app (you could put it in environment.rb if you're going to use it a lot, for now I'll place it in my model definition in user.rb:

require 'ar-extensions'

class User

end

Then to use it, it really couldn't be more straightforward. Use the following in your controller or rake task to import your data:

fields = [:first_name, :last_name, :email]

data = [["glenn", "gillen", "foo@bar.com"],
        ["john", "jones", "jim@bar.com"],
        ["steve", "smith", "bar@foo.com"]]
User.import fields, data

And we're done, 3 new rows have been inserted into the users table, with just the single query. Any more questions, the RDocs are here

Hi, I'm Glenn! 👋 I'm currently Director of Product @ HashiCorp, and we're hiring! If you'd like to come and work with me and help make Terraform Cloud even more amazing we have multiple positions opening in Product ManagementDesign, and Engineering & Engineering Management across a range of levels (i.e., junior through to senior). Please send in an application ASAP so we can get in touch.