I just got back from the Lone Star Ruby Conference. It was really good, and I picked up some nice bits that I was able to work into a rails app for work during periods of downtime at the conference.
James Edward Gray II did a talk on hidden ruby gems. One of the gems he discussed was RBTree (red-black tree). It’s very nearly a drop-in replacement for Hash when you need to sift through some data but don’t want to put it (or can’t put it) into a table. It’s very fast, too.
The Rails Envy guys also did a training session on Advanced ActiveRecord. I did not go to that session, but my cohort did. They discussed ar-extensions as a way of inserting a lot of data at once quickly.
Unfortunately ar-extensions is not compatible with Rails 2.0.x. Actually it ended up being fine because I didn’t really need all that it does, and I didn’t want to risk stomping on ActiveRecord in general for this 1 piece of functionality.
Instead I hacked my own bulk insert. It turns out that in SQL you can do something like this:
INSERT INTO fishes (id, fish) SELECT 1,"Gold" UNION SELECT 2,"Carp" UNION SELECT 3,"Cat"
The syntax is different across the databases, but all you need to do is take the data that you would have done this on:
# Create an Array of new objects
Fish.create([{:fish => 'Gold'}, {:fish => 'Carp'}])
…and rip through the array to create the SQL. Then you can do this: ActiveRecord::Base.connection.execute(sql)
Bam! Lots of inserts with no callbacks firing. Key generation may be different across different databases and scenarios, too, so you probably won’t need to supply the key.