简体   繁体   中英

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. I have to update the email and phone fields. For the phone field is easy to set to "00000000000". In the email field i want to take the name stored in first_name field and add "@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.

Another option, assuming you're using a database that supports the CONCAT() function (which most do):

User.update_all("email = CONCAT(first_name, '@email.com'), phone = '00000000000'")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM