Question

Possible Duplicate:
How do I override rails naming conventions?

When you generate new models in Rails, it pluralizes the table name automatically. However, this doesn't always work out well – particularly with words that have non-standard pluralizations.

I created a model called lens. Since rails thought the word was already pluralized, it didn't pluralize the table name in the migration file. No problem – I just edited the file and changed lens to lenses before running the migration.

Unfortunately, I get this error when trying to instantiate the Lens class:

Could not find table 'lens'

I'm assuming this is because Rails thinks the word is already pluralized, so it doesn't try to pluralize it when trying to find the table in the database.

So what do I do? Should I break from convention and just let Rails call the table lens instead of lenses, or is there a way to configure this properly?

Was it helpful?

Solution

You should have a config/initializers/inflections.rb file where you can do something like.

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'lens', 'lenses'
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top