Search code examples
ruby-on-railsmigratedbmigrate

How can I add new attributes into my migrate?


I currently have the migrate thing like:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :encrypted_password
      t.string :salt

      t.timestamps
    end
  end
end

now, if I wanna add two new attributes into this file, one is: t.string :type , and the other one is: t.string :memory_token , how can I do this please?


Solution

  • If you have already run the migration you will have to create a new one.

    rails g migration AddTypeToUsers

    And then in the migration file you can edit in

    change_table :users do |t|
      t.string :type
      t.string :memory_token
    end
    

    Then run a migration rake db:migrate to make the changes

    If you haven't run the migration then you can simply add

      t.string :type
      t.string :memory_token
    

    To that file you have showed us and then run your migration