Multiple row insertion in a faster way
- by Cyborgo
Hi,
I am using rails and I am trying to insert new rows into the table. The data comes from a text file and I created hash with one of the attribute as key. Then I find all the keys that are currently not in the table. Then I need to insert all of them into the table with all the attributes that I saved in hash.
newvals.each{
|x|
author = Author.create(:first => x, :second => hash[x][0], :third => hash[x][1])
author.save
}
This has lot of overhead as it makes an SQL update one at time. The newals array is usually quite big around the size of 20-30k. I know create can insert multiple objects at once, but I cant seem to figure out how. Is there any way to get this done. Thanks.