# Scaffolding w/ Rails 2.0 - Is there a way to do the models first?

**URL:** <https://www.ruby-forum.com/t/scaffolding-w-rails-2-0-is-there-a-way-to-do-the-models-first/125877>\
**Category:** Rails\
**Created:** [January 13, 2008, 8:06am UTC](https://www.ruby-forum.com/t/scaffolding-w-rails-2-0-is-there-a-way-to-do-the-models-first/125877 "2008-01-13T08:06:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![melvinram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@melvinram](https://www.ruby-forum.com/u/melvinram)\
**Post date:** [January 13, 2008, 8:06am UTC](https://www.ruby-forum.com/t/scaffolding-w-rails-2-0-is-there-a-way-to-do-the-models-first/125877/1 "2008-01-13T08:06:41Z")

</div>

I was playing around with the new scaffold generator and I noticed  
that if I generated a model first (which creates a migration) and  
updated the migration for the model… and then tried to do a scaffold  
of the model, the generator won’t create any usable views or  
controller. Essentially, it’s not reading the model or database when  
creating the scaffold.

But if delete the model and the migration… and run the scaffold  
again w/ the data structure of the model specified, it will create a  
usable controller and views.

**My questions is:** Is there a way to create the models first and  
then create the scaffolds… now that the old scaffold generator has  
been removed?

~ mel

---

<div class="post-metadata">

**Author:** ![melvinram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@melvinram](https://www.ruby-forum.com/u/melvinram)\
**Post date:** [January 13, 2008, 9:02am UTC](https://www.ruby-forum.com/t/scaffolding-w-rails-2-0-is-there-a-way-to-do-the-models-first/125877/2 "2008-01-13T09:02:45Z")

</div>

Hi Mel,  
I’m not really a rails pro, but from my understanding, if you create a  
new scaffold, the rails generators will automatically create a  
controller, model and scaffold/view.  
Fairly good in my view, but it does take some getting used to.  
For Example…  
`
rails testing
cd testing
ruby script/generate scaffold MODELNAME
`  
(Of course replacing MODELNAME with a new model name (that hasn’t yet  
been created) like Products)  
This will create these files:  
exists app/models/  
exists app/controllers/  
exists app/helpers/  
create app/views/products  
exists app/views/layouts/  
exists test/functional/  
exists test/unit/  
create app/views/products/index.html.erb  
create app/views/products/show.html.erb  
create app/views/products/new.html.erb  
create app/views/products/edit.html.erb  
create app/views/layouts/products.html.erb  
create public/stylesheets/scaffold.css  
dependency model  
exists app/models/  
exists test/unit/  
exists test/fixtures/  
create app/models/product.rb  
create test/unit/product\_test.rb  
create test/fixtures/products.yml  
create db/migrate  
create db/migrate/001\_create\_products.rb  
create app/controllers/products\_controller.rb  
create test/functional/products\_controller\_test.rb  
create app/helpers/products\_helper.rb  
route map.resources :products

Hope this answers your question…  
Mitch

---

<div class="post-metadata">

**Author:** ![melvinram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@melvinram](https://www.ruby-forum.com/u/melvinram)\
**Post date:** [January 13, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/scaffolding-w-rails-2-0-is-there-a-way-to-do-the-models-first/125877/3 "2008-01-13T17:49:53Z")

</div>

AFAIK, the scaffold generator will only generate the views based on  
what you provide it at the command line,

If you’ve already created your model, don’t sweat! just add the field  
names and types when you generate the scaffold, choose No when it asks  
to overwrite your model migration and fixtures. It will generate the  
views based on what you provided it.

e.g  
ruby script/generate scaffold name:string description:text  
category\_id:integer

would create the views with f.text\_field(:name),  
f.text\_area(:description) and (i wish)  
f.collection\_select(:category\_id, @categories, :id, :name)

not that DRY a solution, but better than writing them yourself. Look  
into make\_resourceful by hampton caitlin, which rocks as a replacement  
RESTful scaffold generator.

Cheers,  
Joe

---

<div class="post-metadata">

**Author:** ![melvinram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a698b9/32.png) [@melvinram](https://www.ruby-forum.com/u/melvinram)\
**Post date:** [January 13, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/scaffolding-w-rails-2-0-is-there-a-way-to-do-the-models-first/125877/4 "2008-01-13T22:24:21Z")

</div>

Thanks Joe. Good suggestion on just specifying the variables again and  
it wouldn’t be that big a deal except they are specified differently  
then in the migration… which means I have to do more than just copy/  
pasting. It is better than creating it all by hand but hopeful they  
address this.

I will check out make\_resourceful. Hope that will address the issue.

Thanks.

~ mel
