Elias7
Elias7

Reputation: 12861

Best way to add attributes to User Model in Ruby on Rails

my User Model looks like:

    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

If I wanted add one more :attribute, is it best to create another migration file for adding a new column (see another Stackoverflow thread) or can I just manually add t.string :name_of_new_attribute and then rake db:migrate?

Thanks!

Upvotes: 6

Views: 8600

Answers (1)

Norto23
Norto23

Reputation: 2269

The proper way is to create a new migration. In the main rails project folder, run

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

and then run rake db:migrate

An alternative to this is to edit the original migration file, reset/destroy the database and re-run all migrations.

Upvotes: 16

Related Questions