Change model definition using scaffold

849 views
Skip to first unread message

Gromski

unread,
Aug 10, 2008, 5:28:26 PM8/10/08
to Ruby on Rails: Talk
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

Ramon Miguel M. Tayag

unread,
Aug 10, 2008, 6:45:43 PM8/10/08
to rubyonra...@googlegroups.com
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.

--
Ramon Tayag

Gromski

unread,
Aug 12, 2008, 7:50:21 PM8/12/08
to Ruby on Rails: Talk
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

On Aug 10, 11:45 pm, "Ramon Miguel M. Tayag" <ramon.ta...@gmail.com>
wrote:

Jeff

unread,
Aug 12, 2008, 9:51:49 PM8/12/08
to Ruby on Rails: Talk
On Aug 12, 6:50 pm, Gromski <grom...@gmail.com> wrote:
> 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...

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

David A. Black

unread,
Aug 12, 2008, 10:50:04 PM8/12/08
to Ruby on Rails: Talk
Hi --

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 :-)


David

--
Rails training from David A. Black and Ruby Power and Light:
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!

Reply all
Reply to author
Forward
0 new messages