Pluralize your own way in Ruby on Rails

I have faced a situation on a project where we needed the distinction between a single 'media' and multiple 'media.' By default, Rails will pluralize 'media' to the same thing:

'media'.pluralize #=> 'media'

In our Rails app, that was unacceptable; we had a Media model, and multiple media could belong to a single other model. We needed a way to distinguish between a single media and multiple media. After some discussion about what we should go with, we decided with 1 'media' and 2 'medias.' How to get Rails to recognize this?

You can create an initializer where you can keep all of your non-default pluralizations: config/initializers/inflectors.rb. In that file you can add the following:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'media', 'medias'
end

Restart your app, and now it should pluralize 'media' correctly:

'media'.pluralize #=> 'medias'

If you don't want Rails to try to pluralize at all, then you can tell the inflector to not count that word with inflect.uncountable 'media'. This means that when .pluralize is called on that string, it will always return that same unchanged string.

Note that the inflectors initializer usually comes with pretty descriptive commented out instructions already in the file to help you along. That's a good resource to turn to, as well as the great documentation out there on the internets.