2011-10-28 44 views

回答

5

您可以使用交易

titles = ["T-Shirt", "Boots", "Cap"] 

ActiveRecord::Base.transaction do 
    titles.each do |title| 
    Thing.create(:title => title) 
    end 
end 

OT做一個SQL查詢:

query = [] 
titles.each do |title| 
    query << "('#{title}')" 
end 
sql = "INSERT INTO things ('title') VALUES #{query.join(", ")}" 
ActiveRecord::Base.connection.execute(sql) 

挺有意思的文章

http://www.coffeepowered.net/2009/01/23/mass-inserting-data-in-rails-without-killing-your-performance/

+0

鏈接被打破 –