Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Callbacks and the Active Record
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 
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

Callbacks and the Active Record
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2010-02-25

    Table of Contents:
  • Callbacks and the Active Record
  • Validating the Format of an Attribute
  • Making Callbacks
  • Reviewing the Updated Models
  • Updating the User Model

  • 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


    Callbacks and the Active Record


    (Page 1 of 5 )

    In this conclusion to a five-part series that delves into the Rails framework's Active Record, you'll finish learning about validations, and take a look at callbacks. This article is excerpted from chapter five of the book Beginning Rails: From Novice to Professional, written by Jeffrey Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).

    Validating the Length or Size

    Sometimes you want to validate the length, or size, of a field entry. You can do this by using thevalidates_length_ofmethod. We’ll use this method in ourUsermodel to specify a valid range of characters for a login name, as shown in Listing 5-22. The option for specifying a range of values is:within.

    Listing 5-22. validates_length_of Method, in app/models/user.rb

    class User < ActiveRecord::Base 
      validates_length_of :login, :within => 4..40
    end

    If you want to ensure only the minimum or maximum, you can use the:minimumor:maximumattributes. Table 5-7 lists thevalidates_length_ofmethod options.

    Table 5-7. Options for validates_length_of

     

     

     

     

     

    Option

    Description

    :minimum

    Specifies the minimum size of the attribute

    :maximum

    Specifies the maximum size of the attribute

    :is

    Specifies the exact size of the attribute

    :within

    Specifies the valid range (as a Ruby Range object) of values acceptable for the attribute 

    :allow_nil

    Specifies that the attribute may be nil; if so, the validation will be skipped. 

    :too_long

    Specifies the error message to add if the attribute exceeds the maximum.

    :too_short

    Specifies the error message to add if the attribute is below the minimum.

    :wrong_length

    Specifies the error message to add if the attribute is of the wrong size

    :message

    Specifies the error message to add if :minimum, :maximum, or :is is violated.

     

     

     

     

     

    More Ruby-on-Rails Articles
    More By Apress Publishing


     

    RUBY-ON-RAILS ARTICLES

    - Recording Acceptance Tests
    - Commands for Acceptance Testing
    - Acceptance Testing
    - Checkout and Order Processing: the User Side
    - Checkout and Order Processing: the Administr...
    - Online Order Processing: Using PayPal
    - Order and Payment Handling for an Ecommerce ...
    - Checkout and Order Processing for an Ecommer...
    - Getting Text and Currency Working for Multip...
    - Translating a Site for Multiple Language Sup...
    - Going Global with Multiple Language Support
    - Multiple Language Support for an Ecommerce A...
    - Protecting Your Rails Ecommerce Application
    - Passwords and More Security for a Rails Ecom...
    - Securing the Login for a Rails Ecommerce App...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek