views:

306

answers:

3

say I want to scaffold generate university

but university should eventually be related to state. as in each university has one state and one state has many universities.

in my scaffold command should I include state_id??

script/generate scaffold university name:string address:string state_id:int

... I know I will have to update my models to have them understand this one to many relationship but I dont think its the best way to just add the column in the database manually.... so my question is...what would be the most appropriate approach? am I doing it correctly? please help.

A: 

That's the way I do it. So yes, unless someone else has a better way.

DanSingerman
A: 

There's no right answer. You can include it when creating the scaffold or you can skip it and add it to the migration later before executing rake db:migrate.

The advantage of specifying the field in the scaffold is that Rails can add a reverse-migration to remove the field and then autogenerated scaffold will include the field in the show/edit view.

Simone Carletti
the field will also be included in the fixtures
DanSingerman
Excellent point, DanSingerman.
Simone Carletti
+1  A: 

As you know, scaffolds aren't meant to be relied on for a final application, they just provide a handy way to autogenerate a starting point for your views.

Probably the easiest way to do it, is to have scaffold generate the migration for the columns that will appear in your views as fields. Then create a second migration for those columns that won't.

script/generate scaffold university name:string address:string
script/generate migration add_status_id_to_universities state_id:integer another_hidden_column:string
rake db:migrate

This works even if you've already migrated once.

P.S. You might want to look at the ActiveScaffold plugin to automatically adapt your views to database changes.

EmFi
This just seems unnecessarily complicated to me.
DanSingerman
ll give him the answer cause he showed me tha its supposed to be integer instead of int... even though he missread state as status
NachoF
@NachoF Sorry, I missed that I've fixed that.
EmFi
@DanSingerman I'd normally just write my own migration to add additional columns, but I thought that solution was unnecessarily complicated. By doing things this way it's no longer necessary to edit 4 files to hide columns from the user.
EmFi