Vij*_*jay 4 mysql ruby-on-rails
在Java中,我们有批处理执行,如下面的java代码:
Statement statement = null;
statement = connection.createStatement();
statement.addBatch("update people set firstname='John' where id=123");
statement.addBatch("update people set firstname='Eric' where id=456");
statement.addBatch("update people set firstname='May' where id=789");
int[] recordsAffected = statement.executeBatch();
Run Code Online (Sandbox Code Playgroud)
如何在rails ActiveRecord中做同样的事情?
你可以尝试一下.看起来像你在追求什么.
# Updating multiple records:
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)
Run Code Online (Sandbox Code Playgroud)
引用:https://cbabhusal.wordpress.com/2015/01/03/updating-multiple-records-at-the-same-time-rails-activerecord/
为了满足岗位要求,它转换为:
people = {
123 => { "firstname" => "John" },
456 => { "firstname" => "Eric" },
789 => { "firstname" => "May" }
}
Person.update(people.keys, people.values)
Run Code Online (Sandbox Code Playgroud)
请注意,将上述内容转换为SQL仍会产生多个查询
| 归档时间: |
|
| 查看次数: |
3366 次 |
| 最近记录: |