The Inflector:: a Stickler for Convention

Ryan Kendig
4 min readJan 12, 2021

The first application I ever built was a CLI database application using ActiveRecord. This was my introduction to creating relationships between models and creating my own migrations. The purpose of the app was a soccer analytics platform for the English Premier League. The three models were fairly straightforward: teams played matches in stadiums. At this stage of my programming experience, I was building my own migrate files and creating table names without the convenience (ie. security blanket) of Rails. I knew that I was going to be referring back to what I had learned so far in bootcamp. I did not, however, think I would need to recall what I had learned in Ms. Zabronsky’s fifth grade English class. Ah, I was so much younger then. And by ‘then’ of course I mean:

the life and times of a student in coding bootcamp.

ActiveRecord works under a particular set of rules, but doesn’t necessarily stop you from breaking them. Let’s go back to one of my models for a moment.

here we have a table that is broken.

Did you know that the pluralized version of the word ‘stadium’ is stadia? I sure didn’t. Did you think that stadia was just a failed attempt at a cloud-gaming service? I sure did. Here’s what happened when I tried to create any instances of the stadium class:

ryankendig@Ryans-MacBook-Pro thisstinks % ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation    "stadiums" does not exist
LINE 5: WHERE a.attrelid = '"stadiums"'::regclass

ActiveRecord has built in logic that is looking for the proper pluralization of the word ‘stadium.’ It took me an embarrassingly long time to realize that ‘stadia’ was the word ActiveRecord wanted, and not ‘stadiums.’ I’ll admit that this isn’t a problem that occurs to many people other than those poor souls who choose table names like ‘medium,’ ‘stimulus,’ or ‘hair’, but it did get me interested in what goes on under the hood, especially as I moved to using Rails.

Ma’am please do not use the Ford Mustang as a pizza oven.

Once you begin using a Rails generator, Rails knows that your models should have a singular name, classes are to be in CamelCase, tables in a ‘nice_table_format,’ and just about everything that isn’t a model should be plural and have the proper pluralization. Rails knows this and many other things using something called ActiveSupport.

Active Support is the Ruby on Rails component responsible for providing Ruby language extensions, utilities, and other transversal stuff.

What was previously filed under ‘Rails magic’ is actually a module that has numerous patterns to manipulate Ruby strings. Going one step further, the component that handles pluralization is ActiveSupport::Inflector.

Here’s an excerpt from the Ruby on Rails documentation:

“The 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.”

The Rails generator written on the command line gets converted into each aspect of the application in the proper format. That’s pretty neat, but surely Rails can’t get everything right, especially pluralization. To test this, you can call Inflector methods to see how a word will be output. Here are a few I found enjoyable:

I seem to have forgotten my cashes at home, I’ll have to run home on my foots.

Even Rails magic can’t beat the dysfunctional and sometimes chaotic nature of inflection in the english language, and that’s okay. There’s a file in your application where you can specify your own inflection rules => config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|inflect.irregular 'stadium', 'stadiums'inflect.irregular 'goose', 'geese'inflect.uncountable %w( cash )end

There are a handful of methods in order to write new rules. I’ve chosen ‘irregular’ and ‘uncountable.’ With ‘irregular’ you can manually choose pluralization of a word. ‘Uncountable’ will ignore inflection all together. Enter your Rails console to and now you have a different result:

2.6.1 :001 > "stadium".pluralize=> "stadiums"2.6.1 :002 > "goose".pluralize=> "geese"2.6.1 :003 > "cash".pluralize=> "cash"

In summary, ActiveSupport::Inflector takes simple Ruby strings and applies grammar and formatting rules so that they play nice with each aspect of the Model View Controller design pattern. Inflector makes decisions for you, for better or for worse. In the case of the latter, customized rules can be added to handle any outliers you may come across. If you’re interested in algorithm design pertaining to english pluralization, this is a great read from Australian computer scientist, Damian Conway.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ryan Kendig
Ryan Kendig

Written by Ryan Kendig

Player of guitars and games. Learning to code one curly brace at a time.

No responses yet

Write a response