Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - Options for Web Applications with Ruby on ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Options for Web Applications with Ruby on Rails
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-03-29

    Table of Contents:
  • Options for Web Applications with Ruby on Rails
  • 15.5 Displaying Templates with Render
  • 15.6 Integrating a Database with Your Rails Application
  • 15.7 Understanding Pluralization Rules

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

       · This article is an excerpt from the book "Ruby Cookbook," published by O'Reilly. We...
     

    Buy this book now. 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.

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT