Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a field after I've run rails generate model/scaffold?

Let's say I forgot to add a field to my model. How do I go about adding it cleanly? Do I need to re-run rails generate or can I edit a file somewhere?

like image 533
cjm2671 Avatar asked Jan 26 '11 14:01

cjm2671


1 Answers

Either you should re-run your generate scaffold/model (will blow away existing code)

or you can add the field directly to the database and your view

Rails3: rails generate migration add_column_name_to_table_name column_name:string

or

Rails2.x: ruby script/generate migration addColumnToTableName column_name:string

which will generate a file in db/migrate which you can apply with a 'rake db:migrate'

Then you should modify your views to add the appropriate code to display/edit the new field.

more info: http://railscasts.com/episodes/83-migrations-in-rails-2-0

like image 195
Christopher Johnson Avatar answered Nov 16 '22 01:11

Christopher Johnson