Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 949
Test Test Avatar asked Dec 05 '11 20:12

Test Test


2 Answers

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.

like image 140
huntsfromshadow Avatar answered Oct 19 '22 07:10

huntsfromshadow


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.
like image 40
lucapette Avatar answered Oct 19 '22 07:10

lucapette