Rails Magic: The Inflector

Daniel Glover
3 min readFeb 27, 2019

Someone has said it, programming can seem magical at times. This week, I was introduced to Rails and it definitely feels magical. Rails is a web-application framework and it comes with so many neat and kind tools to help the developer. While using Rails, I find myself wondering how does this magic work?

Rails is all about convention — convention over configuration — and that’s something to keep in mind. We can use Rails to generate pretty much the majority of our web-application (routes, controllers, models, tables, views — you name it!) and somehow Rails knows what their structure’s going to be and how they should be named. Part of the magic behind this, is in the inflections library. The Inflector is an extension of ActiveSupport, which is a component tasked with providing language extensions and utilities for Rails. My interpretation of this, is that it makes our life even easier. It interprets the name of what we’re generating and is tasked with producing and placing that keyword is needed in our program.

Let’s use a Rails generator…

‘rails g resource Student name:string module:integer hungry:boolean’

The Rails generator has built our files and understood when and where the keyword ‘student’ needs to be transformed. (placed & pluralized if necessary.)

Here’s a quote 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.”

(https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html)

This is how the Rails magic knows how and when to pluralize our keyword. This reminds me of the helper methods I was building for my first project, wherein I had tailored methods to automatically render where the text should sit in my command-line app. The keyword working alongside ActiveSupport’s Inflection extensions is weaving and connecting our files together. Saving us precious time, and energy on manually inserting this information.

Below is a screenshot of the ROR documentation explaining how the Inflector tableize works:

The tableize method transforming our class_name argument inside the

Sometimes the inflections library might not know the proper plural form for a word, but we can build custom inflections to achieve what we want. The ActiveSupport::Inflector extensions won’t be updated anytime soon, as the Rails core team has stated that they wanted the inflections library to work for all legacy applications relying on older versions of Rails. So, customization is the way forward if we need to fix anything.

There’s still so much “magic” to Rails — to web development in general — but its important to remember that it isn’t magic, just a lot of clever work pieced together to make something “magic”. Don’t feel intimidated by not understanding that magic, just peel away at the layers one by one.

--

--