Renan  Protector
Renan Protector

Reputation: 183

Rails creating wrong table name for model

I'm creating a model named Stadium with rails g model Stadium but rails is converting table name to stadia.

I've manually changed table name to stadium in the migration, and added set_table_name "stadium" in my model class. But all my routes are looking for stadia_path.

I've tried rails g model Stadium --force-plural with no success.

I think I should use an inflection for this, but I don't know how to create this inflection. Stadium is singular, I still need the plural stadiums for this model.

Upvotes: 2

Views: 1544

Answers (2)

Dennis Hackethal
Dennis Hackethal

Reputation: 14305

The dictionary says that both "stadiums" and "stadia" are correct (given that the word is Latin, "stadia" as plural for neuter words ending in -um seems legitimate).

Just add this to config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'stadium', 'stadiums'
end

You can set up all kinds of irregular plurals here.

Destroy your old generation by doing

rails d model Stadium

and generate it again doing

rails g model Stadium

You will see how it magically creates files like this:

db/migrate/20130330130335_create_stadiums.rb

Then run the migration and you're set!

Upvotes: 11

uday
uday

Reputation: 8730

For inflections you have to something like this in the config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'stadium', 'stadiums'
end

hope it helps!

Upvotes: 2

Related Questions