Recently, I had to insert a ton of sample data into our test database to facilitate testing. My first attempt was to use the activerecord-import gem. Looks pretty promising but for some odd reason I just couldn’t get it work from inside a Rake task. It kept inserting data in sequential order instead of doing BULK inserts.
Well, after some R&D, I came across a “simpler” solution that doesn’t require the use of any additional gems. It turns out that all I had to do was wrap all of my inserts inside one transaction. This speeds up the inserts by a HUGE factor when compared with sequential inserts – ~200 ms vs. 1000 ms for about 10 records! An additional benefit of this approach is that all of your model validations still run as they normally would.
Below is a code snippet:
def make_users puts "Creating users..." users = [] 10.times do |n| users << User.new( full_name: "User #{n}", email: "user_#{n}@test.com", password: "foobar", password_confirmation: "foobar") end User.transaction do users.each { |u| u.save } end puts "Finished creating users." end
Bye bye, activerecord-import gem! I am sticking with the transaction wrapper!

