Rails 3,将模型中的字段从字符串更改为日期时间类型 [英] Rails 3, changing field in model from string to datetime type
本文介绍了Rails 3,将模型中的字段从字符串更改为日期时间类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在制作模型时,我将行类型设置为字符串(当时我不知道日期时间).目前,我有很多记录已填满该行.是否有通过迁移将模型的行转换为日期时间的安全方法-除了删除它,然后再添加回去?
When making my model, I made row type as string (I wasn't aware of datetime at the time). Currently I have plenty of records that have that row filled. Is there a safe way to convert the model's row to datetime through a migration - besides just removing it, and then adding it back?
谢谢!
推荐答案
根据文档,这是一个在日期时间转换字符串的迁移示例.我不确定它是否可以正常工作,因此您可能需要先在dev/stage env上试用一下,然后再进入prod env.
Accordingly to the documentation this is an example of migration that transforms a string in datetime. I'm not sure it will work so you may want to try it out on a dev/stage env before going to the prod env.
class ChangeColumnToUsers < ActiveRecord::Migration
def self.up
change_column :users, :created_at, :datetime
end
def self.down
change_column :users, :created_at, :string
end
end
这篇关于Rails 3,将模型中的字段从字符串更改为日期时间类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文