【问题标题】:Best way to add attributes to User Model in Ruby on Rails在 Ruby on Rails 中向用户模型添加属性的最佳方法
【发布时间】:2012-04-16 01:17:21
【问题描述】:

我的用户模型如下所示:

    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

如果我想再添加一个 :attribute,最好创建另一个迁移文件来添加新列 (see another Stackoverflow thread) 还是我可以手动添加 t.string :name_of_new_attribute 然后 rake db:migrate?

谢谢!

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

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

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

    然后运行rake db:migrate

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

    【讨论】:

    • 那么当我以后想添加更多属性时,我可以简单地编辑这个迁移文件吗?
    • 您最好再创建一个包含新属性的迁移
    猜你喜欢
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    相关资源
    最近更新 更多