Change model definition using scaffold

Hi all

Apologies if this a trivially newb question but I'm new to rails (and
frameworks in general) but is there a way to change the model
definition using the scaffold, i.e. is it possible to update the
model, views etc if I decide I need to add field d to fields a, b and
c that I already created using script/generate scaffold...?

At the moment I'm having to delete the generated code, rollback the DB
migration ad generate it from scratch again - I'm sure there must be
an easier/faster way.

Cheers

Mark

I understood the question's answer seems to be:

yes, you can edit the files themselves to reflect the changes you made
to the model. If you added a new field x to the model, then you can
change the files like _form.html.erb to have the x field in it.

Hi Ramon

Not quite. I understand that the files can be edited 'manually' but I
was wondering if there was a short hand using the scaffold so that I
wouldn't have to, e.g. I re-run script/generate with a different param
to add a field to all of the relevant files...

Cheers

M

No, but you could:

1. Revert your migration with rake db:migrate:rollback (to go back one
version; or use VERSION= explicitly)
2. script/destroy scaffold ModelName

Then you can script/generate your scaffold again.

But that's a lot of work... easier to just manually update a few
files. If you first refactor your new and edit forms out into a
partial, you'll save yourself some editing.

Jeff

REST with Rails
Saturday, Oct 4, 2008
Austin, Texas
http://www.purpleworkshops.com/workshops/rest-and-web-services

Hi --

Hi all

Apologies if this a trivially newb question but I'm new to rails (and
frameworks in general) but is there a way to change the model
definition using the scaffold, i.e. is it possible to update the
model, views etc if I decide I need to add field d to fields a, b and
c that I already created using script/generate scaffold...?

At the moment I'm having to delete the generated code, rollback the DB
migration ad generate it from scratch again - I'm sure there must be
an easier/faster way.

In most cases if you want to modify an existing schema you would
create a new migration file:

   script/generate migration add_email_column_to_user

and then put what you need in the newly-created file and then run
"rake db:migrate".

If you're at the very beginning of a project, and decide you want to
change something, you might migrate backwards and start again. Once
the project is underway, though, migrating backwards ends up being
more confusing than it's worth in almost every case.

As for the scaffolding, I wouldn't recommend using it as the basis for
a real application, except in cases where what it produces is exactly
what you want. If you find yourself developing an app by starting with
the scaffolding and making lots of changes (or wondering whether what
you're doing is OK because it deviates from the scaffolding), it's a
sign that you should abandon the scaffolding and just develop the app.
Or you can skip that process and just abandon the scaffolding from the
start :slight_smile:

David