Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - Ruby-on-Rails: Understanding the Basics of...
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

Ruby-on-Rails: Understanding the Basics of Active Record
By: APRajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2006-10-11

    Table of Contents:
  • Ruby-on-Rails: Understanding the Basics of Active Record
  • Active Record: What is it?
  • Active Record: Steps to using it
  • Rails in the Real World

  • 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


    Ruby-on-Rails: Understanding the Basics of Active Record - Active Record: What is it?


    (Page 2 of 4 )

    Active Record is an ORM framework or layer distributed along with RoR. Even though the term distributed is used, the Active Record is built into the core of RoR. Since Active Record is an ORM layer, it provides the following mapping services:

    1. Tables to Classes

    2. Columns to Attributes

    3. Primary Keys to Ids

    4. Rows to Objects

    The major difference between Active Record and other ORM frameworks/layers is the way mapping is done. Most of the popular ORM frameworks such as Hibernate use XML as the mapping container. However Active Record uses a convention-over-configuration methodology. Let me show you how it is done.

    Tables to Classes

    To map a table to a class, the class has to be derived from ActiveRecord::Base i.e. the Base class present within the ActiveRecord package. What essentially happens when  a class is derived from ActiveRecord::Base is that the derived class acts as a wrapper for the database table. In understanding the table’s name, Active Record assumes that the pluralized name of the class is the name of the table. If the class name has multiple capital letters, it is assumed that the table name contains underscores between the words. Some of the examples are:

    Table Name

    Class Name

    Orders

    Order

    Line_Items

    LineItem

    Data

    Datum

        This behavior can be turned off by the following two steps:

    • Set the global flag ActiveRecord::Base.pluralize_table_names to false. This flag is present in the environment.rb file in the config directory.
    • Override the default generation of a table name using the set_table_name directive.

    For example, if a table named Orders is to be mapped to the class Order the code would be:

    class Order < ActiveRecord::Base end

    But if the class Order needs to be mapped to a table Order_QA then the code would be:

    class Order <ActiveRecord::Base set_table_name "Order_QA" # Not "Orders" end

    Now let's see how columns are mapped to attributes.

    Columns to Attributes

    Once a table has been mapped to a class, there is no requirement to explicitly map the columns to the attributes. That’s because Active Record determines the attributes of a table dynamically at runtime. Typically, Active Record reflects on the schema to configure the class that wraps the table. The following shows how the SQL data types are mapped to Ruby’s data types:

    Next let's look at how Primary key is mapped.

    Primary Key to Ids

    If you remember the example application from the first part of this series, the table’s Primary key was named id and had the data type of integer. Though the logical one would have been the Order_id, yet this id was used. At first glance, this might be considered preposterous. However, if one looks at the long run, the id as the Primary Key has its benefits.

    Let's say that the Order_Id is based on a 16 digit format including the Item id, user id, and so forth. In the future if the Order_Id has to be increased to 20 digits by including the last 4 digits of an RFID based code, all the dependent table’s columns would have to be changed. That’s a large order for even a medium-sized application. The mapping is done dynamically. However, if the requirement is to map an existing  Primary Key then it can be done as follows:

    class Order <ActiveRecord::Base set_primary_key "orderId" end

    Rows to Objects

    Whenever a retrieve operation is performed on a class, the corresponding SQL query is fired at the database and the row is retrieved, which is then used to populate the object of the same class. The values of the column in the retrieved row become the values of attributes of the object. In a nutshell, the row is mapped into the object. For example, the following returns an Order object having the id of 27:

     

    an_order = Order.find(27)

     

    That covers mapping. Now let's see the steps involved in performing CRUD operations using Active Records.

    More Ruby-on-Rails Articles
    More By APRajshekhar


     

    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 1 hosted by Hostway
    Stay green...Green IT