In rails3.0 how to insert multiple records in a table? plz tell me any example application.
You can use transactions
titles = ["T-Shirt", "Boots", "Cap"]
ActiveRecord::Base.transaction do
titles.each do |title|
Thing.create(:title => title)
end
end
Ot make one sql query:
query = []
titles.each do |title|
query << "('#{title}')"
end
sql = "INSERT INTO things ('title') VALUES #{query.join(", ")}"
ActiveRecord::Base.connection.execute(sql)
Quite interesting article
http://www.coffeepowered.net/2009/01/23/mass-inserting-data-in-rails-without-killing-your-performance/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With