2011-10-28 13 views

risposta

5

È possibile utilizzare le transazioni

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

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

Ot fare domanda uno sql:

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

Molto interessante articolo

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

+0

il link è rotto –

Problemi correlati