Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

Rails Conventions and Naming

Learn the Rails naming rules for models, tables, controllers and files, how inflections pluralize words, and how Zeitwerk autoloading finds classes. Includes runnable Ruby stand-ins.

Rails feels like magic because it guesses so much from names. Write class Article in a file called article.rb in the right folder and Rails already knows the table, the controller, the route helpers and the view folder. The guessing follows strict rules. Once you learn them, you can predict file names and error messages with no documentation.

This lesson collects the naming rules, explains inflections (the singular and plural logic) and shows how Zeitwerk, the autoloader, turns a constant name like Admin::ArticlesController into a file path. The runnable examples are stand-ins written in plain Ruby, because the real ActiveSupport inflector is not available on the runner.

The naming table

Thing Style Example
Model class CamelCase, singular BlogPost
Model file snake_case, singular app/models/blog_post.rb
Database table snake_case, plural blog_posts
Controller class CamelCase, plural + Controller BlogPostsController
Controller file snake_case, plural app/controllers/blog_posts_controller.rb
View folder snake_case, plural app/views/blog_posts/
Foreign key singular model name + _id blog_post_id
Join table both plurals, alphabetical, joined by _ categories_posts
Test file model name + _test test/models/blog_post_test.rb

Two rules cover most cases: models are singular, tables and controllers are plural, and Ruby classes use CamelCase while files and database names use snake_case.

If you cannot decide between singular and plural, ask: "is this one thing or a collection?" A model describes one row, so User. A controller handles the collection of users, so UsersController. The table stores many rows, so users.

Inflections: singular and plural

Rails has an inflector that converts words: "person".pluralize gives "people", and "categories".singularize gives "category". It follows English rules plus a list of irregular words. You can use these methods anywhere in Rails code. Read-only examples:

Ruby
# read-only: ActiveSupport string methods
"article".pluralize        # => "articles"
"person".pluralize         # => "people"
"BlogPost".underscore      # => "blog_post"
"blog_post".camelize       # => "BlogPost"
"blog_posts".classify      # => "BlogPost"
"BlogPost".tableize        # => "blog_posts"
"blog_post".humanize       # => "Blog post"
"blog_post".titleize       # => "Blog Post"

When the built-in rules guess wrong, you add your own in config/initializers/inflections.rb:

Ruby
# read-only
ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular "octopus", "octopi"
  inflect.uncountable %w[equipment metadata]
  inflect.acronym "API"
end

Stand-in: a small inflector

This is a deliberately small version of the real thing. It shows the structure: rules, irregular words and uncountable words, all as plain data.

Ruby Stand-in: pluralize and singularize with rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The real inflector has many more rules and handles acronyms, but you now know how tableize and classify produce a table name from a class and back.

Zeitwerk: names that find files

Rails loads your code with Zeitwerk. It maps constant names to file paths and loads a file the first time its constant is used. No require needed. The rule: a file app/models/blog_post.rb must define BlogPost, and a nested constant Admin::ArticlesController must live in app/controllers/admin/articles_controller.rb.

flowchart LR
  A["Admin::ArticlesController<br/>(constant)"] -->|"underscore each part"| B["admin/articles_controller.rb"]
  B -->|"prefix autoload root"| C["app/controllers/admin/articles_controller.rb"]

Zeitwerk maps in both directions: constants to files and files to constants.

If the names do not match, you get a NameError or a Zeitwerk::NameError ("expected file ... to define constant ..."). bin/rails zeitwerk:check finds these mismatches before they reach production.

Stand-in: constants and file paths

Here is the mapping in both directions, as plain Ruby.

Ruby Stand-in: constant name to file path and back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The last line shows the classic mistake: the file blogpost.rb would have to define Blogpost, not BlogPost.

Other conventions worth knowing

  • Primary key: every table has an integer id column (bigint in new apps).
  • Timestamps: created_at and updated_at are filled in automatically.
  • Boolean columns: name them published and ask article.published?.
  • Foreign keys: author_id points at authors.id, and belongs_to :author uses it (associations).
  • Routes: resources :articles expects ArticlesController (resourceful routes).
  • Method names: predicates end in ?, dangerous methods end in !, matching standard Ruby (methods).

Do not name a model after a Ruby class such as Class, Object or Module, and do not add a column called type unless you want single table inheritance: Active Record uses that column to pick the subclass. Choose names like Category, Kind or Tier instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the conventional table name for a model called BlogPost?

    Table names are snake_case and plural: blog_posts.
  2. 2. Which file must define Admin::ArticlesController under Zeitwerk?

    Each :: becomes a directory, and each name is underscored: admin/articles_controller.rb.
  3. 3. Where do you teach Rails that octopus pluralizes to octopi?

    Custom inflection rules go in config/initializers/inflections.rb.
  4. 4. A comments table that belongs to posts uses which foreign key column?

    Foreign keys are the singular name of the parent model plus _id, so post_id in the comments table.

Summary

  • Models are singular CamelCase; tables, controllers and view folders are plural snake_case.
  • The inflector (pluralize, singularize, tableize, classify) converts between forms, and you add exceptions in config/initializers/inflections.rb.
  • Zeitwerk loads constants from file paths, so file name and constant name must match.
  • bin/rails zeitwerk:check finds naming mismatches.
  • Foreign keys, timestamps and primary keys follow fixed names too.

Next: Environments and Configuration