How do you modify a model you've generated using modeling? For example, the model myModel originally had columns a, b and c, but I now want to add column d.
Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.
I generally use it. Because it saves lot of time as it generate good amount of code and in most case error free. So I will suggest you to use scaffolding in RoR. And the generated is easy to understand so can update it as your need.
Rails 3 and above use the following code :
rails generate migration add_fieldname_id_to_tablename fieldname:string
Rails 2
ruby script/generate migration add_fieldname_to_tablename fieldname:string
This no longer works and returns the following error in Rails 3:
ruby: No such file or directory -- script/generate (LoadError)
ruby script/generate migration add_fieldname_to_tablename fieldname:string
this is the shortcut method to do exactly what you want. if you need more control, or if you have a lot of columns to add, Andrew H's answer will work fine too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With