Rails build and save in bulk
I've got something like the following:
module Bar < ActiveRecord::Base
belongs_to :foo
...
module Foo < ActiveRecord::Base
has_many :bars, dependent: :destroy
def build_bars
1000.times do |i|
bars.build(num: i)
end
end
def create_default_bars!
build_bars
save
end
Notice that Foo#build_bars is cheap. Even though it loops 1000 times, it
takes very little time. But then, once you hit save, suddenly ActiveRecord
decides to perform 1000 inserts, which is incredibly slow.
How can I write a custom save_the_bars method such that it performs a
single bulk INSERT query for all of the bars I have built up (I remind
you, the 1000 builds are apparently very cheap), but which functions as a
drop-in replacement for save in this example?
I'm expecting an answer along the lines of recommendation #3 of this blog
post:
https://www.coffeepowered.net/2009/01/23/mass-inserting-data-in-rails-without-killing-your-performance/
But since my example uses build and relies on some slightly nontrivial
rails magic, it isn't immediately obvious how to translate it over. Bonus
points for benchmarks!
No comments:
Post a Comment