[英]How to pass a variable parameter value using the update_all() method in Rails?
I'm trying to use the #update_all method to bulk update a huge amount of data at high speed in a rake task.我正在尝试使用 #update_all 方法在 rake 任务中高速批量更新大量数据。 I have to update the email and phone fields.我必须更新 email 和电话字段。 For the phone field is easy to set to "00000000000".对于电话字段很容易设置为“00000000000”。 In the email field i want to take the name stored in first_name field and add "@email.com".在 email 字段中,我想获取存储在 first_name 字段中的名称并添加“@email.com”。 I've been trying things like this:我一直在尝试这样的事情:
User.update_all(email: "#{User.first_name}"+"@email.com", phone: "00000000000")
But it doesn't work.但它不起作用。 Any ideas?有任何想法吗?
User.update_all("email = first_name || '@email.com', phone = '00000000000'")
||
will concatenate strings.将连接字符串。 The first name of each user will be concatenated with the @email.com and assign to the email field.每个用户的名字将与@email.com 连接并分配给 email 字段。
Another option, assuming you're using a database that supports the CONCAT()
function (which most do):另一种选择,假设您使用的数据库支持CONCAT()
function (大多数都支持):
User.update_all("email = CONCAT(first_name, '@email.com'), phone = '00000000000'")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.