# Scaffolding for pre-existing database table in 2.0.1

**URL:** <https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612>\
**Category:** Rails\
**Created:** [December 10, 2007, 4:16am UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612 "2007-12-10T04:16:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [December 10, 2007, 4:16am UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/1 "2007-12-10T04:16:54Z")

</div>

Hi,

first of all I will apologize in advance for my presumably noobish  
question, but I’m only starting to learn Rails and am a little  
confused with all the changes in 2.0.1. There are as good as no  
tutorials out yet and the 2 or 3 screencasts I’ve seen deal with the  
creation of both the app and the database.

Now my problem is that I already have a database with a fair amount of  
records in it (contacts with columns id:integer, first\_name:string,  
last\_name:string). In order to get a simple CRUD interface (that’s all  
I want for now), with the pre-2.0 version of Rails, I could simply  
“generate scaffold contacts” and the framework would provide me with  
all the necessary files to get started with (controller, model and  
views).

When I try the same command (“generate scaffold contacts”) in 2.0.1 it  
will almost do the same thing, but somehow it misses all the fields  
from the database. The list-view solely contains a long listing of  
“Show Edit Destroy” lines (no first or last name listed here). When I  
click on Edit, the form just provides me with an update button. So no  
fields for first- and last name here either.

Is it supposed to be this way now in 2.0.1? If so, what can I do to  
bring back the old functionality?

Regards,

Sebastian

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [December 10, 2007, 4:43am UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/2 "2007-12-10T04:43:15Z")

</div>

New way is to specify the fields.

script/generate scaffold person first\_name:string last\_name:string

On Dec 10, 2007 1:46 PM, Sebastian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> records in it (contacts with columns id:integer, first\_name:string,  
> click on Edit, the form just provides me with an update button. So no  
> fields for first- and last name here either.
> 
> Is it supposed to be this way now in 2.0.1? If so, what can I do to  
> bring back the old functionality?
> 
> Regards,
> 
> Sebastian
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [December 10, 2007, 4:53am UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/3 "2007-12-10T04:53:11Z")

</div>

Hey Ryan,

thanks for the quick answer.

I tried that multiple times before via the generator function in the  
RadRails IDE and it didn’t work (nothing happened, no error message).  
It simply didn’t create any controllers/views/models.  
Just now I tried it again via the windows console, et voilà, ROR  
creates all the classes like it used to. So it seems that this  
malfunction was due to a bug/2.0-incompatibility in RadRails. Hope,  
they’ll fix this soon!

Thanks again,

Sebastian

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [December 10, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/4 "2007-12-10T16:47:17Z")

</div>

On 12/9/07, Ryan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> New way is to specify the fields.
> 
> script/generate scaffold person first\_name:string last\_name:string

And if the table already exists you might want to add --skip-migration

script/generate scaffold person first\_name:string last\_name:string  
–skip-migration

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [December 11, 2007, 7:22am UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/5 "2007-12-11T07:22:02Z")

</div>

Ok, been following this all day.

Scaffolding is really not great for production, but it is nice to have  
it  
build the forms for you. It saves me some typing at least, and I find  
that’s  
what most people are after with the scaffolding. With that in mind, I  
took  
some of the old scaffolding code and made it into a gem. It’s designed  
to  
work off of a pre-existing model.

sudo gem install scaffold\_form\_generator

Then just do

ruby script/generate scaffold\_form User users

It will build these files

/app/views/users/new.html.erb  
/app/views/users/edit.html.erb  
/app/views/users/\_form.html.erb

The form builder skips created\_at, updated\_at, lock\_version, and  
anything  
with \_id at the end. It also makes boolean fields checkboxes instead of  
true/false dropdowns.

Learn more at

[http://scaffoldform.rubyforge.org](http://scaffoldform.rubyforge.org)

Now, it doesn’t do “everything” completely RESTful yet but it’s a really  
good start. If you have suggestions, let me know. It’s not going to ever  
be  
extended to handle relationships - I looked into it and it’s not worth  
trying - too hard to know what you are trying to relate, what you want  
to  
show in your dropdowns, etc.

-bph

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [December 10, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/6 "2007-12-10T21:27:25Z")

</div>

I did this yesterday. You _HAVE_ to specify --skip-migration in case  
its an existing model, else it will stop the whole “generate scaffold”  
task before it creates the controller.

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 8, 2008, 6:14am UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/7 "2008-01-08T06:14:54Z")

</div>

Ryan B. wrote:

> New way is to specify the fields.
> 
> script/generate scaffold person first\_name:string last\_name:string

I’m considering Ruby on Rails, but I already have a big database schema,  
and I’d rather not re-type firstname:string lastname:string, for the  
dozens of fields in my dozens of tables. Is there some way for Ruby to  
build the scaffold based on what it finds in the existing database?  
Wasn’t that the whole point in previous versions? This seems like a big  
step backwards…

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 8, 2008, 6:21am UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/8 "2008-01-08T06:21:21Z")

</div>

… and one more comment/question… how can I prevent Ruby from  
pluralizing my model. Say my table is called “game.” If I try this new  
scaffold with…

ruby script/generate scaffold game name:string

… what I get in my file structure is “games” (plural), but the table  
in my existing database is “game” (singular). I then get SQL errors on  
like “Table db.games doesn’t exist.”

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 8, 2008, 6:20am UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/9 "2008-01-08T06:20:51Z")

</div>

The idea is that Rails doesn’t know which of your fields you want to  
include, or what kind they are.

It’ll be easier for you to type them and design your own layout.

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 8, 2008, 6:43am UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/10 "2008-01-08T06:43:06Z")

</div>

Not too sure on that, I was sure that scaffolding named everything  
correctly.

On Jan 8, 2008 3:51 PM, Mr Mapes [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 8, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/11 "2008-01-08T20:08:19Z")

</div>

On Jan 8, 8:38 am, “Brian H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Finally, Rails is not about scaffolding. That was a marketing trick to get  
> people sucked in. Most people who do Rails professionally don’t use  
> scaffolding at all, as it often generates a bunch of code you don’t need, or  
> is not complex enough to handle the basic tasks.

I do (or did) use scaffolding. It’s probably a unique situation: I  
work for a government agency that has a large number of legacy  
datatables which just need maintenance API’s stuck onto them to do  
CRUD on our intranet.

Some of these monsters have a bajillion fields, and I can erase code a  
whole lot faster than I can type it. So, I used “scaffold Monster” all  
the time.

I’m sure you’re right about most people who do Rails professionally,  
though. If I were making standalone websites, like most folks,  
scaffolding might be a distraction. I’d do paper prototypes first,  
then code as needed in that situation.

I really miss the “old school” scaffolding – in fact, I may recreate  
it and call it something else like scaffold\_api or scaffold\_crud or  
something. Thanks for the scaffold\_form\_generator, by the way – it  
put back a lot of what I missed the most.

Ron

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 8, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/12 "2008-01-08T20:34:49Z")

</div>

Ron:

Well, in that case, check out my gem.  
[http://scaffoldform.rubyforge.org](http://scaffoldform.rubyforge.org)

It uses the old-style model reflection to build just the form (new,  
edit,  
and \_form.html.erb) It doesn’t generate models or controllers… just  
the  
views for the form. I did this for exactly the reason you outlined…  
generating a form can be handy. I basically took the old code from  
Rails  
1.2.3 and made it work with 2.0, with a few minor exceptions: anything  
with  
\_id is not generated as a field, created\_at and updated\_at fields are  
skipped, and anything that’s a boolean gets a checkbox instead of a  
dropdown.

Maybe that will help.

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 8, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/13 "2008-01-08T20:36:01Z")

</div>

lol I missed the end of your reply 🙂 Looks like you already are using  
it.  
Do you have any suggestions for additions to it?

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [January 8, 2008, 2:39pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/14 "2008-01-08T14:39:45Z")

</div>

On Jan 7, 2008 11:21 PM, Mr Mapes [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> You’re not following conventions, which is explained by your use of the  
> legacy schema. Rails assumes that table names are plural and model names  
> are  
> singular.

What you actually need to do is run the scaffold as such:

ruby script/generate scaffold game name:string

Then open up app/models/game.rb

Add the “set\_table\_name” method call to tell the model to use the  
singular  
table.

class Game \< ActiveRecord::Base  
set\_table\_name “game”  
end

If your primary key is not called “id” then you can specify that as  
well. Be  
warned though - Rails likes integers for its keys.

class Game \< ActiveRecord::Base  
set\_table\_name “game”  
set\_primary\_key “game\_id”  
end

Finally, Rails is not about scaffolding. That was a marketing trick to  
get  
people sucked in. Most people who do Rails professionally don’t use  
scaffolding at all, as it often generates a bunch of code you don’t  
need, or  
is not complex enough to handle the basic tasks.

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [December 6, 2012, 7:09pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/15 "2012-12-06T19:09:47Z")

</div>

Mr Mapes wrote in post #613756:

> Ryan B. wrote:
> 
> > New way is to specify the fields.
> > 
> > script/generate scaffold person first\_name:string last\_name:string
> 
> I’m considering Ruby on Rails, but I already have a big database schema,  
> and I’d rather not re-type firstname:string lastname:string, for the  
> dozens of fields in my dozens of tables. Is there some way for Ruby to  
> build the scaffold based on what it finds in the existing database?  
> Wasn’t that the whole point in previous versions? This seems like a big  
> step backwards…

Probably to late for Ryan B. problem, but  
there is a cool gem called schema\_to\_scaffold to generate a scaffold  
script.  
it outputs: rails g scaffold users fname:string lname:string bdate:date  
email:string encrypted\_password:string  
from your schema.rb our your renamed schema.rb. Check [here](https://rubygems.org/gems/schema_to_scaffold)

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [March 27, 2009, 3:49pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/16 "2009-03-27T15:49:27Z")

</div>

Mr Mapes wrote:

> Ryan B. wrote:
> 
> > New way is to specify the fields.
> > 
> > script/generate scaffold person first\_name:string last\_name:string
> 
> I’m considering Ruby on Rails, but I already have a big database schema,  
> and I’d rather not re-type firstname:string lastname:string, for the  
> dozens of fields in my dozens of tables. Is there some way for Ruby to  
> build the scaffold based on what it finds in the existing database?  
> Wasn’t that the whole point in previous versions? This seems like a big  
> step backwards…

I’m a little late to the party here.

Try this from script/console

> > TableClass.columns.each { |c| puts c.inspect }; nil

You’ll see a list of all the columns in the table, along with the data  
types and names and so forth. It should be a pleasurable afternoon to  
write a script that consumes this and generates a “script/generate  
scaffold” command.

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [August 29, 2014, 7:41pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/17 "2014-08-29T19:41:22Z")

</div>

Just another quick solution. You can generate the Ruby  
scaffolding commands using SQL out of the database. Just access  
sys.columns or whatever your particular database provides  
(INFORMATION\_SCHEMA or something). For each column, generate that line  
that Ruby wants for a column / table entry. Then you can select as you  
want for the Ruby app deleting unwanted column / table lines.

```
   Does anyone have a definitive Ruby generation command for Nifty

```

using Ruby 4, for an existing database? I could generate the entire  
thing via SQL, which I would be happy to provide.

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [August 29, 2014, 7:40pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/18 "2014-08-29T19:40:51Z")

</div>

Sebastian wrote in post #599469:

> Hi,
> 
> first of all I will apologize in advance for my presumably noobish  
> question, but I’m only starting to learn Rails and am a little  
> confused with all the changes in 2.0.1. There are as good as no  
> tutorials out yet and the 2 or 3 screencasts I’ve seen deal with the  
> creation of both the app and the database.
> 
> Now my problem is that I already have a database with a fair amount of  
> records in it (contacts with columns id:integer, first\_name:string,  
> last\_name:string). In order to get a simple CRUD interface (that’s all  
> I want for now), with the pre-2.0 version of Rails, I could simply  
> “generate scaffold contacts” and the framework would provide me with  
> all the necessary files to get started with (controller, model and  
> views).
> 
> When I try the same command (“generate scaffold contacts”) in 2.0.1 it  
> will almost do the same thing, but somehow it misses all the fields  
> from the database. The list-view solely contains a long listing of  
> “Show Edit Destroy” lines (no first or last name listed here). When I  
> click on Edit, the form just provides me with an update button. So no  
> fields for first- and last name here either.
> 
> Is it supposed to be this way now in 2.0.1? If so, what can I do to  
> bring back the old functionality?
> 
> Regards,
> 
> Sebastian

```
   Just another quick solution. You can generate the Ruby 

```

scaffolding commands using SQL out of the database. Just access  
sys.columns or whatever your particular database provides  
(INFORMATION\_SCHEMA or something). For each column, generate that line  
that Ruby wants for a column / table entry. Then you can select as you  
want for the Ruby app deleting unwanted column / table lines.

```
   Does anyone have a definitive Ruby generation command for Nifty 

```

using Ruby 4, for an existing database? I could generate the entire  
thing via SQL, which I would be happy to provide.

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [October 3, 2014, 4:04pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/19 "2014-10-03T16:04:26Z")

</div>

Email Address:removed\_email\_address@domain.invalid  
Hi,

I’m developing a heavy project using Ruby on Rails, and I already have a  
big database schema in my sublime text,but my database has a dozen of  
fields of tables which are sometimes very tiresome when scaffolding such  
as £rails g scaffold profile firstname:string lastname:string,  
…Please show me any other alternative on how to handle such  
scenario…

I ll be glad to know.

Thanks…

---

<div class="post-metadata">

**Author:** ![sebastian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@sebastian](https://www.ruby-forum.com/u/sebastian)\
**Post date:** [October 3, 2014, 5:13pm UTC](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612/20 "2014-10-03T17:13:30Z")

</div>

Email Address:removed\_email\_address@domain.invalid\>\>\>Jason Fleetwood-Boldt

@Jasonfb, i guess i did alot of research on ruby on rails as far  
scaffolding is concern, but you find when it comes on real world  
boilerplate applications…it’s kind of a nightmare when we try oldschool  
method ‘scalfolding’ because it may take you actually the whole day  
scaffolding even one table that contains over 100 colum fields…may be  
tell me some procedures on how to use activescaffold…!!

[Next page](https://www.ruby-forum.com/t/scaffolding-for-pre-existing-database-table-in-2-0-1/122612.md?page=2)
