【发布时间】:2013-02-05 15:57:47
【问题描述】:
我是 Rails 的新手;我的问题是: 我有一个用户表,我需要添加更多字段...
我把它放在哪里?
我尝试将其放入迁移文件中,但运行 rake db:migrate 时架构没有改变。
这是在我的迁移文件中:
def self.up
create_table :users do |t|
t.string :username, :null => false # if you use another field as a username, for example email, you can safely remove this field.
t.string :email, :default => nil # if you use this field as a username, you might want to make it :null => false.
t.string :crypted_password, :default => nil
t.string :salt, :default => nil
t.string :nombres, :default => nil
t.string :apellidos, :default => nil
t.string :codigo, :default => nil
t.string :fecha, :default => nil
t.string :zona, :default => nil
t.string :institucion, :default => nil
t.string :frecuencia, :default => nil
t.string :pregunta, :default => nil
t.string :respuesta, :default => nil
t.timestamps
end
架构仍然没有新字段
create_table "users", :force => true do |t|
t.string "username", :null => false
t.string "email"
t.string "crypted_password"
t.string "salt"
t.string "nombres"
t.string "apellidos"
t.string "codigo"
t.string "fecha"
t.string "zona"
t.string "institucion"
t.string "frecuencia"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "remember_me_token"
t.datetime "remember_me_token_expires_at"
end
我该怎么办?
【问题讨论】:
-
创建一个new迁移。几乎所有 Rails 教程都会讨论这个问题。或发帖;谷歌对我的第一个点击是this exact question。
-
谢谢,这解决了我的问题。!!但我有一个疑问,如果我制作一个新的迁移文件来解决我的问题,以后我可以毫无问题地删除该文件吗?因为字段是创建的......在这种情况下,如果我想将我的应用程序放在主机中,我只需要让我的主体迁移文件包含我需要的所有字段?
-
不,不要不要删除您的迁移;它们是您回滚的方式。也没有理由删除它们。
-
但是如果我将应用程序移到主机上,我可以再次制作包含所有字段而不是 2 个文件的迁移文件,1 个包含一些字段,另一个包含更正和更多文件......就是这样我的情况:)
-
何必呢?而且您将无法回滚。在您更熟悉该框架之前,请不要这样做。
标签: ruby-on-rails field