Using Pluralize in your Models in Rails

One of the really nice helpers in Rails is pluralize. It basically provides a really simple way of finding the plural form of a word, courtesy of ActionView::Helpers::TextHelper.

pluralize(1, 'person')
# => 1 person


pluralize(2, 'person')
# => 2 people


pluralize(3, 'person', 'users')
# => 3 users

If you want access to the pluralize functionality outside of your views, you can simply reference the method on ActionController::Base.helpers directly. (You can’t reference the method on ActionView::Helpers::TextHelper directly since that is a module.)

ActionController::Base.helpers.pluralize(2, 'person')
# => 2 people

If – like me – you simply need to pluralize a given word, you can use the method on ActiveSupport::Inflector directly.

ActiveSupport::Inflector.pluralize('user')
# => users

Happy coding.