The correct use of singular vs plural nouns is something the Rails community is very familiar with. Your ‘User’ object resides in the “users” table. A User has_many ‘blogs’ while a Blog belongs_to a single ‘user’ You all know this already.
So with this much attention being paid to number in the back-end code, it’s pretty sloppy to disregard it in the views. “You have 1 friends” or “Last update: 1 weeks ago” just looks bad. Sure, we could write the code to fix it fairly easily, but we have better (ie. more fun) things to work on, right?
Luckily, Rails’s text helpers ride to the rescue once again. The pluralize method does exactly what we’re talking about. Simply give it a number and a singular spelling, and it will use either the singular or plural form based on the number. If it guesses the wrong plural form (ie. ‘mouse’ vs ‘mice’), you can explicitly supply the plural version.
You currently own < %= pluralize @blogs.size, 'blog' %>.
If you have a single blog, it will say “You currently own 1 blog.” For n > 1, it will say “You currently own n blogs.”
Simple and sweet, nothing more to say
November 18th, 2007 at 11:46 am
Cool…