CSC/ECE 517 Fall 2013/ch1 1w43 av

From Expertiza_Wiki
Jump to navigation Jump to search

Introduction

Object-relational mapping (ORM) provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence. For Ruby, several object-relational mapping options are available. This wiki concentrates more on the comparison of ORMs and provides a high level overview of the top Ruby ORMs: ActiveRecord ,Sequel and DataMapper. It also includes a minor discussion on alternative frameworks that can be used either in place of or along with ORMs. These include the persistence framework iBATIS (more specifically the Ruby version RBatis).

Overview

Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space.

ORM features constitutes the following:

  • It binds an object to its data in the database.
    • Class instance variables to database columns
    • Class instances to table rows
  • It depicts associations, properties and behaviors related to tables and their fields.
    • Example, :has_many, :belongs_to associations in ActiveRecord
    • Inheritance cases are mapped to tables
  • It manages the process of converting data between its database and object forms.
  • It performs validation of data before it is persisted to table storage.
  • It generates the SQL for a relational database to perform Create Read Update Delete operations in response to the changes made to the data objects within the application.

The diagram below depicts a simple mapping of an object to a database table….

The Need for ORM

A relational database has been the preferred mechanism for storing data requiring large storage space. An RDMBS provides users efficient create, read, update and delete operations. The efficiency of ORM technique of exchanging data between a relational db and an object oriented language surpasses the traditional techniques of data exchange.

The previously popular database products like structured query language had the disadvantage that they can operate over scalar values that have been organized in tabular format. This is where object relational mapping comes in the picture. It converts the object values into values suitable to be stored in the database. ORM helps in mapping between the logical business model and physical storage model . This gives the user the opportunity to model entities based on actual business requirements rather than the structure of the database. Object Relation mapping basically determines how objects and their relationships are persisted in permanent data storage.

A database ORM is an abstraction of a database. Few popular Ruby ORMs include Active Record, Data Mapper and Sequel. By enabling database rows to be used as objects, the program can more easily access and use the information in a way that is internally consistent and easy to understand. The ORM gives the programmer to manipulate data with the programming language, instead of having to manipulate each attribute as its data type as obtained by the database management system.

When applied to Ruby, implementations of ORM often leverage the language’s metaprogramming strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality. With the addition of Rails, the ORM becomes much more important, as it is necessary to have an ORM to connect the models of the MVC (model-view-controller) stack used by Ruby on Rails with the application's database. Since the models are Ruby objects, the ORM allows modifications to the database to be done through changes to these models, independent of the type of database used. Following are some of the popular ORM tools.

Overview

ActiveRecord is the Object-Relational Mapping system. Ruby on Rails web application framework uses this system.

  • It is based on “convention over configuration” in order to provide defaults to meet most developers needs.
  • For instance, if you create a table in your database names user and add a Ruby class called User to your Rails application, ActiveRecord can figure out that the two are connected.
  • The table can then be used for searching instances and returning specific values.
  • It will also auto generate unique ids for each instance and will even generate attributes that will track, for instance, creation of a row and latest update of a row.

Naming Conventions

By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created.

Model/class Table/schema User users Userpost user_posts Person people

Creating an ActiveRecord model:

Step 1.Create a subclass of ActiveRecord::Base

class User < ActiveRecord::Base
end

class User < ActiveRecord::Base
belongs_to: category
end

belongs_to indicates the presence of a foreign key.


Step 2. Consider a table users created using SQL

CREATE TABLE users (
   uid int(11),
   uname varchar(255),
   PRIMARY KEY(uid)
);

Step 3. You can now perform the following actions

u = User.new
u.name = "Mike"
puts p.name # "Mike"

CRUD operations:

All these methods are of active record and not of all ruby objects in general.

Create

Create method call will create and save a new record into the database:

car= Car.create(name: "Skoda", year: 2000)

New method call creates a new record but does not save it.

car = Car.new
car.name = "Skoda"
car.year=2000

A call to user.save will commit the record to the database

If you don’t save, primary key returns nil or new record returns true

Read

cars=Car.all

car=Car.first

skoda=Car.find_by(name: ‘Skoda’)

Update

To update a particular entry

car = User.find_by(name: 'Skoda')

car.update(name: 'Skoda A1')

Destroy

user = User.find_by(name: 'Skoda')

user.destroy

Once you destroy an object, you can’t modify/update it but you can access it.

Validation

You can validate the state of a model before it gets written into the database.

class Car < ActiveRecord::Base
  validates :name, presence: true
end

You can check for presence, uniqueness etc. on the attributes