Home arrow Ruby-on-Rails arrow Page 4 - Delving Deeper into the Active Record with Ruby-on-Rails
RUBY-ON-RAILS

Delving Deeper into the Active Record with Ruby-on-Rails


In addition to allowing on-the-fly schema mapping, the Active Record provides a variety of services helpful to complicated table situations. Keep reading to learn more.

Author Info:
By: A.P.Rajshekhar
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
November 28, 2006
TABLE OF CONTENTS:
  1. · Delving Deeper into the Active Record with Ruby-on-Rails
  2. · Relationship Mapping
  3. · Acts As Mapping
  4. · Mapping and Rails in the Real World

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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