Ruby-on-rails 如何将多条记录插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5803705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert multiple records into database
提问by Amal Kumar S
How can I insert multiple records into a database using rails syntax.
如何使用 rails 语法将多条记录插入到数据库中。
INSERT INTO users (email,name) VALUES ('[email protected]','a'),('[email protected]','b'),
('[email protected]','c');
This is how we do it in MySQL. How is this done in Rails?
这就是我们在 MySQL 中的做法。这在 Rails 中是如何完成的?
采纳答案by Salil
I use following in my project but it is not proper for sql injection. if you are not using user input in this query it may work for you
我在我的项目中使用了以下内容,但它不适用于 sql 注入。如果您没有在此查询中使用用户输入,它可能对您有用
user_string = " ('[email protected]','a'), ('[email protected]','b')"
User.connection.insert("INSERT INTO users (email, name) VALUES"+user_string)
回答by Jesse Wolgamott
Check out this blog post: http://www.igvita.com/2007/07/11/efficient-updates-data-import-in-rails/
查看这篇博文:http: //www.igvita.com/2007/07/11/efficient-updates-data-import-in-rails/
widgets = [ Widget.new(:title => 'gizmo', :price => 5),
Widget.new(:title => 'super-gizmo', :price => 10)]
Widget.import widgets
Depending on your version of rails, use activerecord-import 0.2.6 (for Rails 3) and ar-extensions 0.9.4 (for Rails 2)
根据您的 rails 版本,使用 activerecord-import 0.2.6(对于 Rails 3)和 ar-extensions 0.9.4(对于 Rails 2)
From the author: http://www.continuousthinking.com/tags/arext
回答by Peter Brown
While you cannot get the exact SQL that you have there, you can insert multiple records by passing create or new on an array of hashes:
虽然您无法获得确切的 SQL,但您可以通过在散列数组上传递 create 或 new 来插入多条记录:
new_records = [
{:column => 'value', :column2 => 'value'},
{:column => 'value', :column2 => 'value'}
]
MyModel.create(new_records)
回答by snakemw
Just a use activerecord-import gem for rails 3 or ar-extensions for rails 2
只需为 rails 3 使用 activerecord-import gem 或为 rails 2 使用 ar-extensions
https://github.com/zdennis/activerecord-import/wiki
https://github.com/zdennis/activerecord-import/wiki
In Gemfile:
在 Gemfile 中:
gem "activerecord-import"
In model:
在模型中:
import "activerecord-import"
In controller:
在控制器中:
books = []
10.times do |i|
books << Book.new(:name => "book #{i}")
end
Book.import books
This code import 10 records by one query ;)
此代码通过一个查询导入 10 条记录;)
or
或者
#@messages = ActiveSupport::JSON.decode(@content)
@messages = JSON(@content)
#prepare data for insert by one insert
fields = [:field1, :field2]
items = []
@messages.each do |m|
items << [m["field1"], m["field2"]]
end
Message.import fields, items
回答by hengsokly
In People_controller.rb
在 People_controller.rb 中
# POST people
NAMES = ["Sokly","Nary","Mealea"]
def create
Person.transaction do
NAMES.each do |name|
@name = Person.create(:name => name)
@name.save
end
end
end
回答by Sergey Potapov
You can use Fast Seederto do multiple insert.
您可以使用Fast Seeder进行多次插入。
回答by Jonhnny Weslley
Just pass an array of hashs to the create method like this:
只需将散列数组传递给 create 方法,如下所示:
User.create([{:email => "foo@com", :name => "foo"}, {:email => "bar@com", :name => "bar"}])

