How to handle irregular plurals in Ruby on Rails
Tags: ruby, rails
Published: 2022-03-31 19:10:28

Join me as I wander down the rabbit hole of word-nerdery in Rails!

One of the key philosophies of Rails is "convention over configuration," which makes it easier for programmers to establish relationships between items in a more or less intuitive way. Consider for a moment what the alternative would be: an emphasis on *configuration* would require explicit instructions to the application in order for it to understand that, for example, the `Student` class represents the data in the `students` table. However, Rails' emphasis on *convention* establishes the relationship based on the singular-to-plural relationship of `student` to `Student`. Provided programmers follow the agreed-upon convention, it all happens under the hood.

This is all well and good, but what happens when the thing you want to keep track of in your database is *not* a regular noun? Say you have a lot of mice to keep track of. If you create a "Mouse" class, will it be expecting a table called "Mouses?"

Fortunately, it will not. Rails has a component called Active Support that takes care of a lot of the irregular cases for you.

FYI: "inflection" is a linguistic term that describes the transformation of a part of a word (usually an ending) depending on its usage and context, for example, to differentiate number, gender, or tense. Verb conjugation is a subtype of inflection; inflections on other parts of speech are called declensions. So Active Support takes care of irregular plurals, but also a wider range of irregular word transformations.

But what about the cases that Active Support doesn't know about? Or the cases it gets wrong? (Or at least, wrong for your purposes—I'm chill about what's right and wrong when it comes to grammar. You do you.) Say, for example, you are building a networking platform for graduates of a fancy university, and it's very important to your boss that the plurals of "alumna" and "alumnus" have their traditional Latin endings, "alumnae" and "alumni." True, these terms will only show up in the back end for you and your team to see, but let's say your boss is a stickler for details.


![Active Support pluralizes "alumna" as "alumnas"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ougz5aqikvidgfe8hp0o.png)

In that case, you can customize Active Support's inflections for your app. As of Rails v7, those customizations are often stored in a file called `/config/environment.rb`, like so:

![ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular 'alumna', 'alumnae'
  inflect.irregular 'alumnus', 'alumni'
end](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oew8swwtw7amlwzbx13h.png) 

This process is also useful and important if you're coding in a language besides English, which is a topic that warrants a blog post of its own (at the least).

As a bonus, Active Support includes other built-in methods that are helpful for modifying words in various ways, including changing from `snake_case` to `camelCase.` A list of these methods can be found [here](https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html). 

Another use case for Active Support is to create custom rules for inputs. For example, if your app has a list of user-inputted names, and you use the method `capitalize()` on the inputs for uniformity's sake it successfully turns "vinay patel" to "Vinay Patel." But it also turns "Phil MacDougal" to "Phil Macdougal," which is incorrect and also a bummer for Phil, who's probably tired of that happening to his name. So you'd want to add a custom rule to Active Support that exempts names starting with "Mac", "Mc", "La", "De," etc.

Good luck and have fun wrangling words in Ruby! 

### Sources consulted/further reading
[Rails API docs](https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html)
[GeeksforGeeks on capitalize()](https://www.geeksforgeeks.org/ruby-string-capitalize-method/)
[This StackOverflow post on irregular plurals in rails (a little outdated)](https://stackoverflow.com/questions/59344290/rails-g-controller-with-irregular-plural)
[Rails Guides: Active Support](https://guides.rubyonrails.org/active_support_core_extensions.html)
[Naming Conventions in Rails](https://medium.com/@lizzardl/naming-conventions-in-rails-ea5fe2f2a51)