Reputation: 51
In rails3.0 how to insert multiple records in a table? plz tell me any example application.
Upvotes: 5
Views: 1280
Reputation: 83680
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
Upvotes: 5