Home arrow Ruby-on-Rails arrow Page 4 - Options for Web Applications with Ruby on Rails
RUBY-ON-RAILS

Options for Web Applications with Ruby on Rails


In this second part of a six-part series on web development and Ruby on Rails, you'll learn how to integrate a database with your RoR application and more. This article is excerpted from chapter 15 of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
March 29, 2007
TABLE OF CONTENTS:
  1. · Options for Web Applications with Ruby on Rails
  2. · 15.5 Displaying Templates with Render
  3. · 15.6 Integrating a Database with Your Rails Application
  4. · 15.7 Understanding Pluralization Rules

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials