Rails – How I get a new model up

Quick tip…

When working on a rails project and I need to get a new model up and running, I do the following.

./script/generate model Fruit

That will generate the model files, and most-importantly an empty migration… all in one step. The migration also has the nice side effect of having the appropriate skeleton of the model already inside (less typing, better consistency == good)

I then edit the migration and run it to generate the necessary modifications to the database. For example, not only will I create the columns for the Fruit object, I may add some columns to another table (Cakes ?) to refer to instances of this new model.

rake migrate

Now, once this is done, I have the model, and the tables ready in the database. The next part is to add the scaffolding.

./script/generate scaffold Fruit \'admin/fruit\'

This will create the controller and view skeleton code that will facilitate the basic CRUD operations.

DONE

For all of this to work, you didn\’t have to do anything but pick a name for the model and choose the fields.

So here is the summary… simple, no?

./script/generate model Fruit

rake migrate

./script/generate scaffold Fruit \'admin/fruit\'

This entry was posted in General. Bookmark the permalink.