In a Rails genealogy app, what goes wrong when a Census model relies on the default English inflections, and how does inflect.irregular fix it?
answer
- words ending in s stay unchanged
- census treated as uncountable
- census_index route key
- has_many :censuses looks for Censuse
- irregular in config/initializers/inflections.rb
basics
~20 sRails' default rules leave census unchanged when pluralizing and singularize censuses to censuse, so a Census model gets a census table, census_index route helpers and a broken has_many :censuses; inflect.irregular "census", "censuses" fixes every derived name at once.
solid answer
~40 sActive Support's English rules pluralize most words ending in `s` by leaving them alone, so `"census".pluralize` is `"census"`, and they singularize by stripping a trailing `s`, so `"censuses".singularize` is `"censuse"`. For a `Census` model that means a `census` table, a model name whose plural equals its singular, so route keys become `census_index`, and `has_many :censuses` looking for a class named `Censuse`. Adding `inflect.irregular "census", "censuses"` in `config/initializers/inflections.rb` teaches both directions at once: the table becomes `censuses`, routes and partials follow, and the association resolves to `Census`. Add it **before** the first migration, and restart the server, because inflections are read at boot. If a `census` table already exists, the model now expects `censuses`, so you either rename the table or set the table name explicitly.
code
ruby · 12 lines# Before the initializer change (default English rules)
"census".pluralize # => "census"
"censuses".singularize # => "censuse"
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular "census", "censuses"
end
# After a restart
"census".pluralize # => "censuses"
Census.table_name # => "censuses"go deeper
Recall that Rails derives table and route names by pluralizing, that some words come out wrong, and that config/initializers/inflections.rb is where you fix them.
Explain how rules, irregulars and uncountables interact, why census breaks in both directions, and what irregular installs.
Plan the fix around existing data: add rules before the first migration, and handle an already-created table with a rename or an explicit name.
Set a team habit of checking domain nouns against the inflector when modelling, so naming mistakes never reach the schema.
## How Rails pluralizes Rails needs singular and plural forms constantly: table names, route helpers, partial paths, association class names. Active Support provides them through **inflection rules** for each locale. The English set has three parts: - **Regular-expression rules**, tried newest first until one matches, such as `y` to `ies` after a consonant or `x`, `ch`, `ss`, `sh` to `...es`. - **Irregulars**, pairs like `person`/`people` and `child`/`children`, which work in both directions. - **Uncountables**, words like `species`, `series` and `sheep` that never change. The built-in set is frozen on purpose: changing a rule in a new release would silently rename tables in existing apps, so mistakes in it are fixed per app, not upstream. ## The `Census` trap For words ending in `s`, the plural rule is "leave the trailing `s` as it is", and the singular rule is "remove a trailing `s`". Neither fits `census`: | Expression | Default result | Intended | |---|---|---| | `"census".pluralize` | `"census"` | `"censuses"` | | `"censuses".singularize` | `"censuse"` | `"census"` | | `Census.table_name` | `"census"` | `"censuses"` | | `Census.model_name.route_key` | `"census_index"` | `"censuses"` | | `Census.model_name.singular_route_key` | `"censu"` | `"census"` | Consequences in a genealogy app: 1. **The table** is created as `census` by the model generator's migration. 2. **Routes and URL helpers**: because the plural equals the singular, `ActiveModel::Name` treats the word like an uncountable and appends `_index` to the collection route key, so `form_with` for a new record looks for `census_index_path`. Worse, the singular route key is computed by singularizing `census`, so `link_to` a saved record looks for `censu_path`. 3. **Associations**: `has_many :censuses` singularizes the association name to find the class, gets `"censuse"`, camelizes it and raises because `Censuse` does not exist. ## The fix: `inflect.irregular` `config/initializers/inflections.rb` exists in every new app with commented examples. Add: ```ruby ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.irregular "census", "censuses" end ``` `irregular` installs rules for **both directions** and removes the words from the uncountable list. After a restart: - `"census".pluralize` returns `"censuses"` and `"censuses".singularize` returns `"census"`; - `Census.table_name` is `"censuses"`, `resources :censuses` routes to `CensusesController`, and `has_many :censuses` finds `Census`. ## Timing matters 1. **Before the first migration**: the generator then writes `create_table :censuses`, and nothing needs renaming. 2. **After tables exist**: the model now asks for `censuses` while the database has `census`. Either rename the table in a migration or pin the old name on the model; both are explicit decisions, not something the inflection does for you. 3. **Restart required**: the initializer runs at boot, and its own header comment says to restart the server after editing it. ## Related tools - `inflect.uncountable` for words that should never change. - `inflect.plural` and `inflect.singular` for regular-expression rules covering a family of words. - Rules are **per locale**; the block above targets `:en`, which is what Rails uses for table and route names. Use `irregular` for single words, which is the common case in domain models: it is precise, symmetric and easy to read. ## Checking a domain noun before you model it A minute in the console before `bin/rails generate model` saves a migration later: 1. Check both directions: `"census".pluralize` and `"censuses".singularize` should round-trip. 2. Check the derived names Rails will actually use: `"Census".tableize` for the table and, once the class exists, `Census.model_name.route_key` for the URL helpers. 3. If either is wrong, add the inflection, restart, and check again before generating anything. Most genealogy nouns are fine: `ancestor` becomes `ancestors`, `family` and `families` round-trip through the consonant-plus-`y` rule, and `child`/`children` and `person`/`people` are built-in irregulars. It is the Latin-looking words ending in `s`, such as `census`, that need attention.
- Why is `Census.model_name.route_key` `census_index` under the default rules?`ActiveModel::Name` compares the singular and plural forms. When they are equal, as with an uncountable word, it appends `_index` to the collection route key so that the collection helper and the member helper do not share a name. `census` pluralizes to itself by default, so it is treated the same way.
- Why doesn't Rails just fix such words in its built-in rules?The default English rules are frozen: changing a rule would change table names, route helpers and association lookups in existing apps on upgrade. Rails expects each app to add the words it needs in its own inflections initializer.
- Rails pluralizes census to censuses out of the box.
- inflect.irregular only affects pluralize, so singularize still needs a separate rule.
- Adding an inflection renames the existing table automatically.
- Inflection changes apply without restarting the server.
- The fix belongs in config/locales/en.yml with the translations.