我有一个包含这些列的存在模型(和表):idnamecity。该表中有数据,我想在此表中添加created_atupdated_at列-谁能帮我一个忙,怎么办?我的意思是-如果创建模型,那么当我保存模型时,这两列会自动创建,并且时间信息总是自动插入。

现在可以在自动插入时间数据的情况下添加这两列吗?

最佳答案

这应该允许您将时间戳列添加到已经存在的模型中

rails generate migration add_timestamps_to_users

这将为您创建一个迁移文件。打开它并进行必要的更改
class AddTimestampsToUsers < ActiveRecord::Migration
  # in my example i'm using `users` table; change this to match your table name
  def change_table :users do |t|
    t.timestamps
  end
end

然后迁移您的数据库
rake db:migrate

10-05 20:33
查看更多