Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Modifying a Model Generated by Scaffolding

Tags:

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.

like image 693
Andrew Hampton Avatar asked Feb 10 '09 02:02

Andrew Hampton


People also ask

What does scaffolding do in Rails?

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.

Should I use scaffold in rails?

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.


2 Answers

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)

like image 110
BookOfGreg Avatar answered Oct 25 '22 06:10

BookOfGreg


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.

like image 26
Luke Avatar answered Oct 25 '22 05:10

Luke