What is ORM in Rails mean?

Reading time ~3 minutes

Object Relational Mapping(ORM)

This is confused topic. I had a friend ask me about the model in Rails, and I spent lots of time trying to explain to him. I suggest those who want to understand this that you need to understand how SQL works.

I quote from rails guide:

Object Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system. Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code.

It means that you can access the database without writing SQL statements. It give the programmers lots of convenience.

But ActiveRecord in the Rails is complex. I use Sequel to understand this topic. I strongly recommend to read Sequel . It is quite similar to ActiveRecord, but with much easier expression. We can learn from it a lot. Some people use sequel-rails to replace the ActiveRecord in the rails.

The SQL part

the three sub language in SQL

  • DDL: Data Definition Language
    • Allows a user to create and modify the schema stored within a database.
    • It includes CREATE TABLE, ALTER TABLE, ADD COLUMN and several other statements for creating or modifying the structure of or rules that govern the data held within thedatabase.
    • This is what we work in migration in Rails
  • DML: Data Manipulation Language
    • This part of the SQL language is what allows a user to retrieve or modify the datastored within a database.
    • This is what we work in Activerecord::Base
  • DCL: Data Control Language
    • Controlling the rights and access roles of the users interacting with the database or table.
    • Methods: GRANT, REVOKE

Schema manipulation

If you read about the migration part in rails,

Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don’t have to write SQL by hand, allowing your schema and changes to be database independent.

In Sequel:

  DB.create_table :items do
    primary_key :id
    String :name, :unique => true, :null => false
    TrueClass :active, :default => true
    foreign_key :category_id, :categories
    DateTime :created_at

    index :created_at

In rails:

  class CreateItems < ActiveRecord::Migration[5.0]
    def change
      create_table :products do |t|
        t.string :name
        t.text :description

Sequel is much like writing the SQL statements. If you are familiar with the SQL, it will remind you of creating a table inside it. But rails is too magic that you forget you create a table by this migration.

I like the author of sequel’s talk in 2009, Here is some take away from his talk, in the page 45 of the slide:

  • Migrations should preferably only do schema modification, no data modification unless necessary
  • Migrations should be self contained, and not reference any part of your app (such as your models)

Sequel ORM

In the page 22 of the slide:

  • Allows easily adding methods to datasets and returned objects
  • Each model class is associated with a single dataset
  • The model class object proxies many methods to its dataset
  • Model instances represent individual rows in the dataset

Remember, each model class is associated with a single dataset!

  require "rubygems"
  require "sequel"

  DB = Sequel.sqlite

  DB.create_table :post do
    primary_key :id
    String :title

  class Post < Sequel::Model

  post = Post.new
  Post.dataset #SELECT * FROM "posts"

There is a table in the database, and we use ORM to represent the table. All our methods here will go into database without directly connecting to it. We can write our own methods in the model.

Other resources:

there is a short intro(5 mins) in the ruby tapas:

to_param in Ruby on Rails

If I want a custom slug=======================I walk through this cutstom slug.1. create migration `add_slug` to add a column inside the...… Continue reading

Why use thin controller with fat model?

Published on June 29, 2017