skip to content

In a Rails genealogy app, which names does a Person model imply for its table, files, controller, routes, views and foreign keys?

level: juniorimportance: must knowfreq 80%

answer

  1. one class name, many derived names
  2. person/people is a built-in irregular
  3. PeopleController in people_controller.rb
  4. resources :people, person_path
  5. person_id as the foreign key

basics

~10 s

A Rails Person model lives in app/models/person.rb and maps to the people table; PeopleController handles resources :people, with helpers like people_path and person_path, templates under app/views/people, and other tables point at it through person_id.

solid answer

~40 s

Rails derives every name from the class name through its inflector. `Person` lives in `app/models/person.rb` and, because `person`/`people` is one of Active Support's built-in irregulars, maps to the `people` table. The controller is `PeopleController` in `app/controllers/people_controller.rb`; `resources :people` routes to it and generates `people_path`, `person_path(person)`, `new_person_path` and `edit_person_path(person)`. Its templates sit in `app/views/people/`, a single record renders the partial `people/_person`, and form params arrive under the `person` key. A model that `belongs_to :person` gets the column `person_id`. Tests and fixtures follow: `test/models/person_test.rb` and `test/fixtures/people.yml`. Break one link, for example by naming the controller `PersonsController`, and you have to configure what the convention would have supplied.

code

ruby · 6 lines
ruby
Person.table_name                   # => "people"
Person.model_name.param_key         # => "person"
Person.model_name.route_key         # => "people"
Person.model_name.singular_route_key # => "person"
Person.new.to_partial_path          # => "people/person"
Event.reflect_on_association(:person).foreign_key # => "person_id"

go deeper

for a junior

Recall the full chain for one model: class and file, table, controller, routes, view folder, params key and foreign key, including that person becomes people.

for a middle

Explain where the names come from: the inflector's rules and irregulars, and ActiveModel::Name with param_key, route_key and to_partial_path.

for a senior

Spot and fix broken links in the chain: hand-named controllers, mis-pluralized words and legacy tables, and choose inflection rules or overrides deliberately.

for a principal

Decide how strictly a codebase follows the chain, and when explicit configuration for legacy data is worth its maintenance cost.

## One name, many derived names Rails code rarely spells out how a model connects to its table, its controller or its templates. Instead, each of those names is **derived** from the class name by Active Support's inflector, and every Rails component uses the same derivation. This chain is the most concrete form of convention over configuration, and interviewers like it because it shows whether a candidate can predict file and table names without looking them up. ## The chain for `Person` | Piece | Name | Derived how | |---|---|---| | Model class and file | `Person`, `app/models/person.rb` | the class name, underscored for the file | | Table | `people` | underscored class name, pluralized | | Foreign key elsewhere | `person_id` | association name plus `_id` | | Controller | `PeopleController`, `app/controllers/people_controller.rb` | plural, camelized, plus `Controller` | | Routes | `resources :people` gives `people_path`, `person_path(person)`, `new_person_path`, `edit_person_path(person)` | route keys from the model name | | Views | `app/views/people/index.html.erb`, partial `people/_person.html.erb` | controller path; `to_partial_path` for a record | | Form params | `params[:person]` | the model name's `param_key` | | Tests and fixtures | `test/models/person_test.rb`, `test/fixtures/people.yml`, `test/controllers/people_controller_test.rb` | mirror of `app/`, fixtures named after tables | ## Why `people`, not `persons` English plurals are not regular, so Active Support ships a small set of rules plus **irregulars**. `person`/`people` is one of the built-in irregulars, together with `man`/`men`, `child`/`children`, `sex`/`sexes`, `move`/`moves` and `zombie`/`zombies`. A handful of words such as `equipment`, `information`, `species`, `series`, `fish` and `sheep` are **uncountable** and never change. Words outside those lists go through regular expressions, and the rule set is deliberately frozen so that upgrading Rails does not rename an existing app's tables. ## `ActiveModel::Name` holds the derived names Every model has a `model_name` object that caches the derivations the rest of Rails asks for: - `singular` `"person"` and `plural` `"people"`; - `element` `"person"` and `collection` `"people"`, which make the partial path `people/person`; - `param_key` `"person"`, the key form params arrive under; - `route_key` `"people"` and `singular_route_key` `"person"`, used by `link_to person` and `form_with model: person` to build URLs; - `i18n_key` `:person`, for translated model names. When `plural` equals `singular`, as for an uncountable word, the route key gets an `_index` suffix so the collection and member helpers do not collide. ## Breaking the chain The chain only works when every link agrees. Typical breaks: 1. **A hand-named controller**: `PersonsController` does not match `resources :people`, so requests to `/people` fail because Rails cannot find `PeopleController`, until you pass `controller: "persons"`. 2. **A word the rules get wrong**: a model whose name the default rules mis-pluralize needs an inflection rule before its table is created, or every derived name inherits the mistake. 3. **A legacy table**: a table called `tbl_person` needs an explicit table-name override on the model, which is a configuration you write because the convention cannot reach it. ## How to check a name quickly In a console, `Person.table_name`, `Person.model_name.route_key` and `Person.new.to_partial_path` show what Rails derived. Generators apply the same rules, so `bin/rails generate scaffold Person` is another way to see the whole chain at once.

  • Which name does `resources :people` expect for the controller, and what happens if you call it `PersonsController`?
    It expects `PeopleController` in `app/controllers/people_controller.rb`, derived from the resource name. With `PersonsController`, requests to `/people` fail because Rails looks up `PeopleController` and cannot find it; you would have to write `resources :people, controller: "persons"`, which is configuration the convention was meant to save.
  • Where does the `person` key in `params[:person]` come from?
    From `Person.model_name.param_key`. `form_with model: @person` names its fields `person[name]`, `person[born_on]` and so on, so the controller reads them under `person`. A namespaced model such as `Admin::User` gets the param key `admin_user`.

  • Rails names the Person table persons by adding an s.
  • The controller for Person is PersonController, singular like the model.
  • Table names have to be declared in each model class.
  • Foreign keys are named after the table, as people_id.
  • Upgrading Rails can silently change how existing words pluralize.