Rails Pluralize Function
Rails pluralize function comes in handy when you need to differentiate "1 person" and "2 people" based on users count. You could just simply do:
pluralize(users.count, 'person')
If you don't want the number shows up, you could do:
'person'.pluralize(users.count)
When users.count == 1, it returns "person", and users.count == 2 => "people"
Now expand that to generate a sentence of "xx person/people is/are going". One way of doing that is through application helper. A helper function is defined as:
def sentence_pluralize(num)
if num == 1
"1 person is going"
else
"#{num} people are going"
end
end
Then you could call that from anywhere in the application to get the correct sentence.
There is another way to achieve this. First add a customer inflection in config/initializers/inflections.rb to pluralize "is"/"are"
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular "is", "are"
end
Now let's put these pieces together. We get "xx person/people" by
pluralize(users.count, 'person')
Then get "is"/"are" (without the number) by
'is'.pluralize(users.count)
In our mockup we have:
<div>
<%=pluralize(users.count, 'person') %>
<%='is'.pluralize(users.count) %>
going
</div>
It gets what we want.