是否有类似Rails中的批处理更新的内容? [英] Is there anything like batch update in Rails?

查看:75
本文介绍了是否有类似Rails中的批处理更新的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我们可以像下面的Java代码一样执行批处理:

In Java we have batch execution like the java code below:

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();

如何在Rails ActiveRecord中做同样的事情?

how to do the same in rails ActiveRecord?

推荐答案

您可以尝试一下.好像你在追求什么.

You can give this a try. Seems like what you're after.

# Updating multiple records:
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)

引用: https://cbabhusal.wordpress.com/2015/01/03/updating-multiple-records-at-the-same-time-rails-activerecord/

为满足职位要求,它翻译为:

To fit the post requirements, it translates to:

people = { 
  123 => { "firstname" => "John" },
  456 => { "firstname" => "Eric" },
  789 => { "firstname" => "May" }
}
Person.update(people.keys, people.values)

请注意,将以上内容转换为SQL仍会产生多个查询

Please note that translating the above into SQL still yields multiple queries

这篇关于是否有类似Rails中的批处理更新的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆