Options for Web Applications with Ruby on Rails - 15.7 Understanding Pluralization Rules
(Page 4 of 4 )
Problem
You want to understand and customize Rails's rules for automatically pluralizing nouns.
Solution
You can use Rails' pluralization functionality in any part of your application, but ActiveRecord is the only major part of Rails that does pluralization automatically. ActiveRecord generally expects table names to be pluralized noun phrases and the corresponding model classes to be singular versions of the same noun phrases.
So when you create a model class, you should always use a singular name. Rails automatically pluralizes:
- The corresponding table name for the model
- has_many relations
- has_and_belongs_to_many relations
For example, if you create a LineItem model, the table name automatically becomes line_items. Note also that the table name has been lowercased, and the word break indicated by the original camelcase is now conveyed with an underscore.
If you then create an Order model, the corresponding table needs to be called orders. If you want to describe an order that has many line items, the code would look like this:
class Order < ActiveRecord::Base
has_many :line_items
end
Like the name of the table it references, the symbol used in the has_many relation is pluralized and underscored. The same goes for the other relationships between tables, like has_and_belongs_to_many.
Discussion
ActiveRecord pluralizes these names to make your code read more like an English sentence: has_many :line_items can be read "has many line items". If pluralization confuses you, you can disable it by setting ActiveRecord::Base.pluralize_table_ names to false. In Rails, the simplest way to do this is to put the following code in config/environment.rb:
Rails::Initializer.run do |config|
config.active_record.pluralize_table_names = false
end
If your application knows specific words that ActiveRecord does not know how to pluralize, you can define your own pluralization rules by manipulating the Inflector class. Let's say that the plural of "foo" is "fooze", and you've build an application to manage fooze. In Rails, you can specify this transformation by putting the following code in config/environment.rb:
Inflector.inflections do |inflect|
inflect.plural /^(foo)$/i, '\1ze'
inflect.singular /^(foo)ze/i, '\1'
end
In this case, it's simpler to use the irregular method:
Inflector.inflections do |inflect|
inflect.irregular 'foo', 'fooze'
end
If you have nouns that should never be inflected (usually because they are mass nouns, or because their plural form is the same as their singular form), you can pass them into the uncountable method:
Inflector.inflections do |inflect|
inflect.uncountable ['status', 'furniture', 'fish', 'sheep']
end
The Inflector class is part of the activesupport gem, and you can use it outside of ActiveRecord or Rails as a general way of pluralizing English words. Here's a stand alone program:
require 'rubygems'
require 'active_support/core_ext'
'blob'.pluralize # => "blobs"
'child'.pluralize # => "children"
'octopus'.pluralize # => "octopi"
'octopi'.singularize # => "octopus"
'people'.singularize # => "person"
'goose'.pluralize # => "gooses"
Inflector.inflections { |i| i.irregular 'goose', 'geese' }
'goose'.pluralize # => "geese"
'moose'.pluralize # => "mooses"
Inflector.inflections { |i| i.uncountable 'moose' }
'moose'.pluralize # => "moose"
See Also
- Recipe 13.11, "Using Object Relational Mapping with ActiveRecord"
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter 15 of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Check it out today at your favorite bookstore. Buy this book now.
|
|