How do I add a field in Ruby on Rails?

ruby, ruby-on-rails

Solution

To add a new column to the database

$ script/generate migration add_fieldname_to_tablename fieldname:string 
$ rake db:migrate

To get your views up to date you can run the scaffold again, with your updated list of fields. It will balk on replacing your migrations but you can force it to replace your views.

$ script/generate scaffold tablename fieldname:string old_field_1:string ...

At the prompt answer a and it will overwrite the views, but not the old migration. It also won't modify your existing data.

Problem

I'm new to RoR, and I've just used scaffold to generate a table and create the pages for CRUD operations. Now I want to add a new field to this. One place I found tells me how to do that in the database, but is there a way to do it where it will add the field to all the pages too, or is that just a manual operation and I need to make sure I know all my fields up front?

Original source