Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top