深入研究文档和 api,我似乎遗漏了如何一次更新多行中的一个字段。
有点像
Table.select(:field).update("update to this").where(id: 4,5,6)
会很好。
这样的东西存在吗?这比必须将所有内容都存储在一个数组中,将其设置为一个值,然后每次都调用 save
要好得多。
最佳答案
您可以使用 update_all方法,例如:
Table.update_all("field = 'update to this'", ["id in (?)", ids])
关于mysql - Rails - 直接在 SQL 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13793458/