Try creating a table for your model

Railers:

Greetings from the low end of the learning curve. I have installed all
the prerequisites on Win32, including a lite MySQL database with a
table in it called Inventory.

Then I run this command line…

ruby script/generate ajax_scaffold Inventory

…and I get this error message:

error Before updating scaffolding from new DB schema, try creating
a table for your model (Inventory)

So I read log/development.log, and it contains this:

Error: Table ‘rails_development.inventories’ doesn’t exist

The system is inexplicably pluralizing my table name. Am I using the
system wrong, or is this a false “feature”?

Can I fix it by adding a view called ‘inventories’? Or must I rename the
table?

And how, in general, can Rails present data views that don’t have the
same shape as the target database schema? Such schemas should be
normalized beyond recognition…

error Before updating scaffolding from new DB schema, try creating
table?

And how, in general, can Rails present data views that don’t have the
same shape as the target database schema? Such schemas should be
normalized beyond recognition…

These might come in handy…

Rails HowTo: Pluralizing
http://www.slash7.com/articles/2005/11/17/rails-howto-pluralizing

Riding Rails: 10 Reasons Rails Does Pluralization
http://weblog.rubyonrails.com/2005/08/25/10-reasons-rails-does-pluralization/

Hi Philip,

The table/model naming isn’t a “false” feature, but a feature :slight_smile: You
don’t
have to create a view or rename the table, though your life will be made
easier if your table names are pluralized. If you want to use a table
name
that Rails doesn’t expect, you can use “set_table_name ‘inventory’” in
your
model class. And yes, you can have models which correspond to views -
I’m
not sure what operations you can perform, but I know you can at least
read
rows from the view.

Philip H. wrote:

Riding Rails: 10 Reasons Rails Does Pluralization
Peak Obsession

And, uh, adding a single line to the default Rails page would have …
prevented the need for such a clever page? And its attendant FAQ?

Phlip wrote:

before Ruby existed…

I may be missing something here (and a search to see your earlier post
reveals that you were using AjaxScaffold) but as I understand it, you
can always create a model without creating the table name in plural.

In the way I know it:
dir>ruby script\generate model Inventory

  • this will create the model ‘inventory’ (app\models\inventory.rb) and a
    migration that will db\migrate\xxx_create_inventory (i think) - this
    file is the one that will actually update the database to create the
    specific table and its fields.
  • you should be able to go to app\models\inventory.rb and add
    set_table_name ‘inventory’ to make it all work.

I may be wrong, but that is my understanding.

It seems that it ‘may’ be a restriction in AjaxScaffold (or perhaps it
will work fine after the model is defined to use the table ‘inventory’)
but it’s certainly not a problem with the way Rails works.

Cheers
Mohit.

@Phlip:

The workflow that should be observed for creating a Rails application is

  1. Create a Rails app (of course)

  2. start with a blank database (no tables0

  3. ruby script/generate model InventoryItems

  4. edit the file db/migrate/001_create_inventory_items.rb

  5. Define your table definition using this file.

  6. run ‘rake migrate’ to build the table in your database.

  7. Test with console
    ruby script/console

    @inventory_item = InventoryItem.new
    @inventory_item.name = “widget”
    @inventory_item.description = “some thing that does some stuff”
    @inventory_item.save
    @inventory_item = InventoryItem.find :first
    @inventory_item.name

  8. Write unit tests using something similar to the console entries
    above.

  9. Edit your app/models/inventory_item.rb file and do any validations,
    etc

  10. Run your tests again.

10 ** OPTIONAL ** ruby script/generate scaffold InventoryItem inventory
11. ruby script/server and test in the browser.

Why InventoryItem? Because I just don’t like using inventories, and it
makes
more sence to me later when I say
store :has_many :inventory_items

Daniel H. wrote:

The table/model naming isn’t a “false” feature, but a feature :slight_smile: You don’t
have to create a view or rename the table, though your life will be made
easier if your table names are pluralized. If you want to use a table name
that Rails doesn’t expect, you can use “set_table_name ‘inventory’” in your
model class.

No I can’t. The model class doesn’t exist until I run generate, which
won’t finish until I pluralize the table name.

Another reason why you will continue to field this FAQ is because, up
until that error message (AND until dumping the log to see what the
real problem was), there’s simply no way for the initiate to guess
they are on-track. I couldn’t tell if I screwed up here, or long
before, and I couldn’t tell if I was at the head of the error cascade.
So even >I< had to ask, despite I have been using
Model-View-Controller, and writing Web servers from scratch, since
before Ruby existed…