在Ruby on Rails中向User Model添加属性的最佳方法

时间:2012-04-16 01:17:22

标签: ruby-on-rails

我的用户模型如下:

    class CreateUsers < ActiveRecord::Migration
       def self.up
         create_table :users do |t|
         t.string :name
         t.string :email

         t.timestamps
       end
    end

      def self.down
        drop_table :users
        end
      end

如果我想再添加一个:属性,最好是创建另一个用于添加新列({3}})的迁移文件,还是只需手动添加t.string:name_of_new_attribute然后rake db:migrate?

谢谢!

1 个答案:

答案 0 :(得分:16)

正确的方法是创建新的迁移。在主rails项目文件夹中,运行

rails generate migration AddDetailsToUser address:string age:integer等......

然后运行rake db:migrate

另一种方法是编辑原始迁移文件,重置/销毁数据库并重新运行所有迁移。