Baz wrote:
I would expect (based on all the old tutorials) that it would create the
MVCs based on the initial migration? But it doesn't seem to do that. It
creates a user model migration that creates the table with a timestamp
only.
Nne of the other fields are there.
If you already have a database then you don't need a migration to create
it. If you want a new database based on the old one, then you can use
the schema dump to create a single migration to create all the tables in
the new database. (Point your database.yml file at the old database, do
the dump, point your database.yml file at the new database, then run the
schema dump.)
At this point, whichever situation applies, you have your database
setup. The script/generate script uses various generators to do things.
Run it without arguments to see the usage. This will list available
generators. Of note at this point are:
controller
- creates a controller and its functional test stub
migration
- creates a stub migration file
model
- creates a model and stubs for unit tests, fixtures and also a
migration file
scaffold
- creates a model, its migration file, its controller, and initial
versions of views for CRUD operations as well as the other support files
mentioned above
Since you have the database, you don't need migrations and you're
probably wanting to start with more than basic model or controller
stubs, so use the scaffold generator for now. The generators won't
extract information from the database. In fact, they are primarily
aimed at initial setup so the intended use is to give the generators the
model structure and create the database from that. For example:
script/generate scaffold Sample name:string description:string
This will create a migration to build a "samples" table with "name" and
"description" columns, along with default timestamp columns (you can
edit the migration to remove them if you don't need them). Also, it
will create the model, a controller and the views for the controller
which will refer to your columns.
In your case, you don't *have* to specify the fields since you won't
need the migrations, so for each model you have, just run:
script/generate scaffold Sample
(where "Sample" is replaced with the model name)
You don't want the migration generated so delete it (or it will cause
problems later if you add migrations for new tables, etc). Note,
however, that by not specifying field names, the views will not
reference them so you will have to edit all your views so that they show
all the data you want them to. You'd probably be doing that anyway as
the default views are all pretty basic. (This is a major change in Rails
2 as in previous versions, the views were created to use reflection and
so newly scaffolded views didn't refer to specific columns at all.)
You'll now have a fully scaffolded application. You then need to edit
all your models to set up relationships, names of primary keys (where it
isn't "id"), etc.
Remember that scaffolding is just to provide a bit of structure to help
you build the application, not to be the application. Also look at the
wiki for other generators you can use which may help.
This isn't really more work than you would have to do when starting from
scratch (as then, you still need to think up your models and fields and
specify them in the generators and apply all the migrations).