A Rails Grammar Lesson

Since I first started learning Rails, one thing that struck me was how incredibly smart the framework is. Not just in anticipating paths and RESTful patterns, but also how class Person magically becomes table People in the database. How in the world does Rails know that it’s not the Persons controller/table? Has somebody hardcoded every possible grammar edge case? I made it my mission to find out.

As this handy blog post points out, if you want to see how Rails would pluralize a word, just run a generator command with the --pretend or -p option:

image

This prints out a list of all the files Rails would create if you ran a generator command without the option. This is also useful if you forget the difference between scaffolds and resources.

Convention is part of what makes Rails so powerful. The framework can anticipate what you need and generate your file structure for you– but at the end of the day, rules were meant to be broken. You can turn off pluralization completely by adding this line of code to your config/environment.rb file:

ActiveRecord::Base.pluralize_table_names = false

But keep in mind that your whole project will break.

You can also experiment with existing conventions by testing pluralization patterns in your Rails console. Use the pluralize and singularize methods:

image

Where do these methods come from? Glad you asked! Rails depends on a gem called ActiveSupport that provides string pluralization patterns (and MANY other things.) Many of the files in the “inflector” directory deal specifically with how and when to pluralize words.

If you love reading source code, the inflector/methods.rb and string/inflections.rb files use higher-level methods like tableize, which depends on the lower-level methods like plural and singular in the inflector/inflections.rb file. The rules are defined in the first place in the lowest-level file, active_support/inflections.rb. It handles both normal and irregular patterns.

image

The Ruby on Rails Cookbook explains how to override the rules provided in the inflector files:

_`$ ruby script/console
>> "foo".pluralize
=> "foos"`

Rails calls words that are the same in both plural and singular form uncountable. To add the word foo to a list of all uncountable words, add the following to the bottom of environment.rb:

config/environment.rb:

`...
Inflector.inflections do |inflect|
 inflect.uncountable "foo"
end`

Reload script/console, pluralize foo again, and you'll find that your new inflection rule has been correctly applied.

`$ ruby script/console
>> "foo".pluralize
=> "foo"`

Other inflection rules can be added to the block passed to Inflector.inflections. Here are a few examples:

`Inflector.inflections do |inflect|
 inflect.plural /^(ox)$/i, '\1\2en'
 inflect.singular /^(ox)en/i, '\1'
 inflect.irregular 'octopus', 'octopi'
 inflect.uncountable "equipment"
end`

These rules are applied before the rules defined in inflections.rb. _
_Because of this, you can override existing rules defined by the framework._

So there you have it, fellow grammar enthusiasts. The Rubyists who came before us took the time to painstakingly define pluralization rules in this file. You can override them if you’d like. In fact, you might have to override them if your models are made-up or non-English words.

image