Forum: Ruby on Rails Rails keeps adding the letter s to the MySQL tablename.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E45e9e4a980897a9668cc9c90080a939?d=identicon&s=25 Jean-paul Ladue (jaypee68)
on 2006-06-06 15:47
I followed this tutorial for Ruby on Rails...

http://www.onlamp.com/pub/a/onlamp/2005/01/20/rail...

Everything went fine.  I created the DB in MySQL and tested the
recipe/new and recipe/list functions, and all went smooth.

Now I'm trying to use Ruby on Rails to track software licenses here at
work and I keep running into this strange problem.  I am setting up the
DB, model, and controller files just fine...

development:
  adapter: mysql
  database: gcsaam
  username: root
  password:
  host: localhost

# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  database: gcsaam
  username: root
  password:
  host: localhost

production:
  adapter: mysql
  database: gcsaam
  username: root
  password:
  host: localhost

softlic.rb

class Softlic < ActiveRecord::Base
end

softlic_controller.rb

class SoftlicController < ApplicationController
	scaffold :softlic
end

So here is my problem.  Ruby or maybe Rails, keeps addign the letter "s"
to the MySQL table name like it shows here...

 ActiveRecord::StatementInvalid in SoftlicController#new

Mysql::Error: #42S02Table 'gcsaam.softlics' doesn't exist: SHOW FIELDS
FROM softlics

RAILS_ROOT: ./script/../config/..

My table name is softlic, not softlics.  I've been at this for a day and
a half and am about to toss something across the room. :-)  I am totally
stumped here.  I even thought maybe it was a bug issue with 1.8.4-17 so
I downgraded to 1.8.2-15.  Does anybody know what my problem might be?
Am I missing some small morsel of info here?  My new project is pretty
much a mirror image of the cookbook example with the exception of
filename changes and different fields in the DB.  Help! :-)  Thank You.
Ba4b0b302231bffb0f685bbef25db0d6?d=identicon&s=25 Bryan Liles (Guest)
on 2006-06-06 15:54
(Received via mailing list)
On Jun 6, 2006, at 9:47 AM, Jean-Paul Ladue wrote:

> DB, model, and controller files just fine...
> # Do not set this db to the same as development or production.
>   username: root
> class SoftlicController < ApplicationController
> FROM softlics
> Am I missing some small morsel of info here?  My new project is pretty
> much a mirror image of the cookbook example with the exception of
> filename changes and different fields in the DB.  Help! :-)  Thank
> You.


You can turn this off by adding the following to your config/
environment.rb file:
ActiveRecord::Base.pluralize_table_names = false
66ca686a219cd292c0e4b8db3ea230bd?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2006-06-06 15:58
(Received via mailing list)
Jean-Paul Ladue wrote:
>
> test:
>   password:
> 	scaffold :softlic
> RAILS_ROOT: ./script/../config/..
>
> My table name is softlic, not softlics.  I've been at this for a day and
> a half and am about to toss something across the room. :-)  I am totally
> stumped here.  I even thought maybe it was a bug issue with 1.8.4-17 so
> I downgraded to 1.8.2-15.  Does anybody know what my problem might be?
> Am I missing some small morsel of info here?  My new project is pretty
> much a mirror image of the cookbook example with the exception of
> filename changes and different fields in the DB.  Help! :-)  Thank You.
>

It's not a bug, it's a feature! :-)

You may have noticed that the ONLamp tutorial used "recipes" as the name
of the table, but accessed it using "recipe" when scaffolding.  RoR
likes to maintain data in tables that are plurals of the  basic record.
So, the class "item" goes into a table called "itemS", class "recipe"
into "recipes" and so on...

Your table needs to be name with an "s" - that's all :)

Or you follow the other mail's suggestion and disable pluralized names.
Cheers
Mohit.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-06-06 16:21
(Received via mailing list)
Hi Jean-Paul,

Jean-Paul Ladue wrote:

> So here is my problem.  Ruby or maybe Rails,
> keeps addign the letter "s" to the MySQL table
> name like it shows here...
>
> Mysql::Error: #42S02Table 'gcsaam.softlics' doesn't exist

> My table name is softlic, not softlics.

> Am I missing some small morsel of info here?  > Rails mailing list

RoR favors convention over configuration.  This is one of the biggies.

Table names are plural version of the model names because a model object
represents one row in a table.  It's a fundamental convention that's a
reflection of the Active Record pattern.  In your case, the table
contains a
collection of software licenses.  Each model object represents one
software
license.  Makes sense, no?

There are ways, as noted by other responders, to over-ride the behavior.
It's not advisable except in extreme circumstances.

Learn it.  You'll love it! ;-)

hth,
Bill
E45e9e4a980897a9668cc9c90080a939?d=identicon&s=25 Jean-paul Ladue (jaypee68)
on 2006-06-06 16:26
Thanks guys, everything is as smooth as silk now.  I had read about the
pluralization but wasn't aware that it worked this way.  Thanks again
and you'll probably see me again soon. :-)
This topic is locked and can not be replied to.