Test Test
Test Test

Reputation: 2889

Delete attribute from model in Ruby on Rails

I generated a User model using this command:

rails generate model User name:string date_birth:date age:int...

As you can see I made a mistake. I entered a date of birth and an age. Now I also migrated into a mySql database. Is there any way to just delete the age field?

Edit: Is it also possible to add a field?

Upvotes: 7

Views: 12916

Answers (4)

tokhi
tokhi

Reputation: 21616

You can also edit your migration file and remove the column which you don't need from the migration file, and then rake db:migrate again.

Upvotes: 0

lucapette
lucapette

Reputation: 20724

At least two choices:

  1. You can run rake db:rollback, delete the field from the migration and then re-run rake db:migrate
  2. You can create a new migration to delete the field with rails generate migration new_migration.

Upvotes: 2

jamiescript
jamiescript

Reputation: 1284

Run the following in terminal

rails destroy model User

This should reverse the generate so you can start again

Upvotes: 0

huntsfromshadow
huntsfromshadow

Reputation: 1085

Create a new migration with

rails g migration migrationname

and in the migration's up method run the following

remove_column :users, :age

run rake db:migrate and you are good to go.

Upvotes: 22

Related Questions