Rails 3 + activerecord,“批量更新"的最佳方式满足条件的所有记录的单个字段 [英] Rails 3 + activerecord, the best way to "mass update" a single field for all the records that meet a condition

查看:18
本文介绍了Rails 3 + activerecord,“批量更新"的最佳方式满足条件的所有记录的单个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 rails 3 中,使用 activerecord,是否有一种单查询方法可以将 :hidden 字段设置为 TRUE 满足条件的所有记录......说例如,:condition =>[ "phonenum = ?", some_phone_number ]

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?

推荐答案

使用 update_all带有可选的第二个条件参数:

Use update_all with the optional second parameter for the condition:

Model.update_all({ hidden: true }, { phonenum: some_phone_number})

这篇关于Rails 3 + activerecord,“批量更新"的最佳方式满足条件的所有记录的单个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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