Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - Delving Deeper into the Active Record with...
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

Delving Deeper into the Active Record with Ruby-on-Rails
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2006-11-28

    Table of Contents:
  • Delving Deeper into the Active Record with Ruby-on-Rails
  • Relationship Mapping
  • Acts As Mapping
  • Mapping and 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


    Delving Deeper into the Active Record with Ruby-on-Rails - Mapping and Rails in the Real World


    (Page 4 of 4 )

    Now let's look how the mapping discussed fits into the whole Rails framework. I will be extending the existing application developed in the previous part by adding a new table and corresponding operations. I will provide a custom view part to show the use of the tree data structure. So let's get started. First comes the table. It's the CATEGORIES table, and here is the DDL for it:

     

    create table categories (

    id int not null auto_increment,

    name varchar(100) not null,

    /* ... */

    parent_id int,

    constraint fk_category foreign key (parent_id) references categories(id),

    primary key (id)

    );

     

    Next comes the model corresponding to the table. The command to generate the model class is:

     

    ruby script/generate model User

     

    In the generated class I am giving the declaration acts_as_tree to tell the framework that I need the tree data structure.

     

        class Category < ActiveRecord::Base

    acts_as_tree :order => "name"

       end

    As you can see, I am reusing the table and model discussed in the previous section. 

    Next comes the controller. The command to create the controller is:

     

    ruby scriptgenerate controller Mapping

     

    class MappingController < ApplicationController

     

    scaffold:category

     

    def list

    end

    end

     

    Every other operation will be provided with a default scaffold except for listing/view. Next comes the definition of listing. Since the application is for a tree structure, the operation for creating the tree will be done in the list method.

     

    class MappingController < ApplicationController

     

    scaffold:category

     

    def list

    @root = Category.create(:name => "Books")

    fiction = root.children.create(:name => "Fiction")

    non_fiction = root.children.create(:name => "Non Fiction")

    non_fiction.children.create(:name => "Computers")

    non_fiction.children.create(:name => "Science")

    non_fiction.children.create(:name => "Art History")

    fiction.children.create(:name => "Mystery")

    fiction.children.create(:name => "Romance")

    fiction.children.create(:name => "Science Fiction")

    end

    end

     

    Next is the list.rhtml. Here is the code for list.rhtml.

     

    <ul>

       <li>

            <%=@root.name%>

               <ul>

                    <li>

                        <% subcat=@root. root.children.first%>

                         <%=subcat.name%>

                      <!-same for all others 

                   </li>

                </ul>

       </li>

    </ul>

     

    That brings us to the end of this part. However, the services provided by Active Record are many. The most important are data validation, transactions and single table inheritance. These will be topics of discussion in the future. Till then...


    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.

       · In this discussion I have focussed on the advanced mapping techniques in Active...
       · I think the first sentence of the last paragraph should read asIn the above...
     

    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