Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Rails way to handle singular/plural possibility?

Tags:

When you present a message to the user that involves an entity that could be either singular or plural, Rails has a shortcut to handle that. I'm talking about this situation:

"Delete committee? Its X meeting(s) will also be deleted."

Rails has a way so that "meeting" is presented as "meeting" or "meetings" depending on X.

I can't remember what that way is. It's not String#pluralize.

like image 850
Ethan Avatar asked Mar 25 '09 05:03

Ethan


2 Answers

ActionView::Helpers::TextHelper::pluralize(count, singular, plural = nil)

Attempts to pluralize the singular word unless count is 1. If plural is supplied, it will use that when count is > 1, otherwise it will use the Inflector to determine the plural form

Examples:

  pluralize(1, 'person')   # => 1 person    pluralize(2, 'person')   # => 2 people    pluralize(3, 'person', 'users')   # => 3 users    pluralize(0, 'person')   # => 0 people 
like image 86
ax. Avatar answered Oct 02 '22 09:10

ax.


In Rails 5

'person'.pluralize(2) # => people 
like image 33
Jeremy Moritz Avatar answered Oct 02 '22 08:10

Jeremy Moritz