Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Datetime 使用db:migrate编辑数据库字段类型_Datetime_Ruby On Rails 3_Dbmigrate - Fatal编程技术网

Datetime 使用db:migrate编辑数据库字段类型

Datetime 使用db:migrate编辑数据库字段类型,datetime,ruby-on-rails-3,dbmigrate,Datetime,Ruby On Rails 3,Dbmigrate,我正在开发一个RubyonRails web应用程序,我对更改我的项目模型中两个字段的类型很感兴趣。创建模型时,我给了两个字段(start\u time和end\u time)int类型,我想将其更改为日期/时间类型 由于我与一个团队合作(可能也是因为这样做是正确的),我想使用rake db:migrate更改这些字段类型。如何创建一个文件来执行此操作?Ruby/Rails中存在的最佳(或唯一)日期/时间类型是什么?运行script/Rails生成迁移UpdateTimeFields,并使用以下

我正在开发一个RubyonRails web应用程序,我对更改我的
项目
模型中两个字段的类型很感兴趣。创建模型时,我给了两个字段(
start\u time
end\u time
)int类型,我想将其更改为日期/时间类型


由于我与一个团队合作(可能也是因为这样做是正确的),我想使用
rake db:migrate
更改这些字段类型。如何创建一个文件来执行此操作?Ruby/Rails中存在的最佳(或唯一)日期/时间类型是什么?

运行
script/Rails生成迁移UpdateTimeFields
,并使用以下命令。(还有一个
change\u column
方法,但我认为它不能在保留任何数据的同时将int列更改为datetime列)

class UpdateTimeFields
我是否可以使用datetime获得任何额外的“功能”,如datetime选择器或至少错误格式的错误消息?当它是datetime字段时,您可以进行适当的验证,轻松查询数据库中的特定时间范围等。对于datetime选择器,它是视图中与数据库字段不直接相关的UI元素。但是,大多数日期选择器将提交与日期时间字段兼容的数据。
class UpdateTimeFields < ActiveRecord::Migration
  def self.up
    rename_column :projects, :start_time, :old_start_time
    rename_column :projects, :end_time, :old_end_time
    add_column :projects, :start_time, :datetime
    add_column :projects, :end_time, :datetime

    # If applicable, insert code to iterate through your existing
    # records and update the new start_time and end_time fields
    # based on your int data.

    remove_column :projects, :old_start_time
    remove_column :projects, :old_end_time
  end

  def self.down
    # do the opposite of above: rename the datetime fields, create the int
    # fields again, migrate the data back into the int fields, and delete
    # the datetime fields.
  end
end