如何在 Rails 3 中为新列设置值?
这可能在另一个问题中,但我找不到它。
我对 Rails 来说是个相对菜鸟。我正在尝试将新列添加到其中已有数据的表中。该列不允许有空值。更新所有现有记录以在这个新列中具有值的最简单方法是什么?我知道它可能在我的迁移的 up 块中,但我不知道语法是什么。
def self.up
change_table :reminders do |t|
t.boolean :active
end
#model name is Reminder - how to update data with a value?
end
This is probably in another question, but I cannot find it.
I am a relative noobie to Rails. I am trying to add a new column to a table that already has data in it. This column is not going to allow nulls. What is the easiest way to update all the existing records to have a value in this new column? I know it is probably in the up block of my migration, but I don't know what the syntax would be.
def self.up
change_table :reminders do |t|
t.boolean :active
end
#model name is Reminder - how to update data with a value?
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做的是添加列,用所需的值更新表中的所有记录,然后将其更改为不允许空值。
What you can do is add the column, update all records in the table with the desired value, then change it to not allow nulls.
试试这个:
Try this: