How is Rails able to find the plural of words while generating scaffolds or controllers?

Shawn Rebello
Take a look at the picture below, nothing complex just a regular scaffold command.

Ruby on Rails Pluralize example
The selected text shows the pluralized version of Record
 As you can see, Rails has automatically pluralized names for views, resource routes, controllers etc, but has not pluralized the model name.

Ever wondered how Rails finds the plural of words while creating controllers, scaffolds, class names etc?

Enter The Inflector..

A module in the ActiveSupport class,  ActiveSupport::Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys.

The default inflections for pluralization, singularization, and uncountable words are kept in inflections.rb.

But what if I don't want the default pluralizations?

Say you want to make a scaffold GunLaws;
 What if you want the model to remain plural and Rails wants to call it GunLaw?

Using rails g scaffold -h we see this option:

[--force-plural], [--no-force-plural]

Neat! So we can use:

rails g scaffold GunLaws --force-plural

Final thoughts

We can also edit  inflections.rb file under config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|

  inflect.uncountable %w(gun_laws)


For more info take a look at the documentation here

Leave a comment if this helped you or if you have any questions!

Shawn Rebello / Author & Editor

Shawn Rebello is an engineering student who likes learning new languages, tools, libraries or frameworks. When he is not coding, he binge watches TV shows and spends time on Wikipedia and Reddit.


Leave a comment here!