Languages

Fsharp
ActionScript
xBase
Clean
GPSS
PureBasic
Sieve
Erlang
JOVIAL
Mercury
Linda
DataFlex
PostScript
FoxPro2
VFP
Cobol
Prolog
Jython
Awk
VisualBasic
JavaScript
Matlab
ASP
Haskell
Csharp
D
Smalltalk
Nemerle
Pixilang
Java
SQL
Python
ObjectPascal
Ruby
Perl
Pascal
Assembler
PHP
C
Functions  Add function  Users  Registration  Enter   About  ASCII Table  Our helpers

Ruby-on-Rails


1 Ruby-on-Rails: Understanding the Basics of Active Record

2 Active Record: What is it?

3 Active Record: Steps to using it

4 Rails in the Real World

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.


1 2 3 4
Study Adds Clue to How Last Ice Age Ended

Photo of melting glacier ice in New Zealand.

As the last ice age was ending, about 13,000 years ago, a final blast of cold hit Europe, and for a thousand years or more, it felt like the ice age had returned.

But oddly, despite bitter cold winters in the north, Antarctica was heating up.

For the two decades since ice core records revealed simultaneous warming and cooling at opposite ends of the planet during this time period, scientists have looked for an explanation.

Results of a new study published this week in the ...

More at http://www.nsf.gov/news/news_summ.jsp?cntn_id=117576&WT.mc_id=USNSF_51&WT.mc_ev=click


This is an NSF News item.

PycckaR
BepcuR


Articles
Articles


Library
Library


Downloads
Downloads

Google Chrome Golf 6
 © Internet, books, teachers and Rudevich Alexander brains.