更新一列,所有行 [英] Update a column, all rows
本文介绍了更新一列,所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表中添加了一个新列,但忘记添加 :default 选项.现在我想在每一行上填充该列.
I added a new column to my table but I forgot to add the :default option. Now I want to populate that column on every single row.
有没有办法使用控制台?过去一个小时我一直在 google 上搜索,但我什么也没找到.
Is there a way to do with using the console? I've been searching google for the past hour but I can't find anything.
我知道如何为单个对象执行此操作,但不知道如何为模型中的所有对象执行此操作.Foo.find(1).update_attribute(:myattribute, 'value')
I know how to do it for a single object, but not for all in a model. Foo.find(1).update_attribute(:myattribute, 'value')
推荐答案
试试这个:
Foo.update_all(some_column: "bar")
这将生成对数据库的 SQL 查询:
This will generate SQL query to database:
UPDATE "foos" SET "some_column" = "bar";
这篇关于更新一列,所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文