Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 + activerecord, the best way to "mass update" a single field for all the records that meet a condition

In rails 3, using activerecord, is there a single-query way to set the :hidden field to TRUE for all records that meet a condition ... say, for example, :condition => [ "phonenum = ?", some_phone_number ]

If a single query cannot do it, what IS the optimal approach?

like image 919
jpw Avatar asked Feb 06 '11 09:02

jpw


1 Answers

Use update_all with the optional second parameter for the condition:

Model.update_all({ hidden: true }, { phonenum: some_phone_number}) 
like image 113
Dogbert Avatar answered Sep 21 '22 20:09

Dogbert