Understanding Pluralization Rules
Problem
You want to understand and customize Railss 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. Lets say that the plural of "foo" is "fooze", and youve 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, its 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. Heres a stand
require ubygems require active_support/core_ext lob.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"
Категории