Rails 3 + activerecord,“批量更新"的最佳方式满足条件的所有记录的单个字段

编程入门 行业动态 更新时间:2024-10-22 20:38:33
本文介绍了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,“批量更新"的最佳方式满足条件的所有记录的单个字段

本文发布于:2023-10-24 13:58:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   批量   条件   方式   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!