【问题标题】:What should I do when I need a new field in a table in rails?当我需要在rails中的表中添加一个新字段时,我应该怎么做?
【发布时间】: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


【解决方案1】:

简单迁移

您可以使用迁移生成器:
$> rails g migration add_position_to_users position:integer
然后运行
$> rake db:migrate

更复杂的迁移

或者更复杂的迁移,rails 也提供:
$> rails g migration add_body_and_pid_to_users body:string:index pid:integer:uniq:index
$> rake db:migrate

有关迁移的更多信息,请访问 railsguides http://guides.rubyonrails.org/migrations.html

【讨论】:

  • 你能帮我解决一个新的疑问吗?我需要表之间的关系,我该怎么做?是这样的i45.tinypic.com/vfjtdj.png 我创建了用户和贷款表,但是我如何创建关系?我希望你能帮助我
  • 对于模型 gist.github.com/itsNikolay/4729133 ,我认为您将能够在您的控制器中创建关联 =)
【解决方案2】:

我认为我们在这里没有提到的是将代码添加到迁移文件中。

我添加列的步骤如下:

# rails generate migration AddFieldName blah:string

生成的迁移文件里面:

(顺便说一句,这通常看起来像:db/migrations/20130330115915_add_field_name.rb)

class AddFieldName < ActiveRecord::Migration
    def change
        add_column :table_name, :blah, :string
    end
end

进行此更改后,我然后运行 db:migrate。然后,将该列添加到数据库中。

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2011-08-14
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多