What is Object Relational Mapping(ORM) in Ruby on Rails ?

Yahya Gok
4 min readApr 11, 2021

--

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.(1*)

ORM it’s a completely ordinary library written in your language that encapsulates the code needed to manipulate the data, so you will NOT use Structured Query Language (SQL) anymore, but directly use an object of your language.(*2)

The Object part is the one you use with your programming language ( ruby object in this case )(*3)

The Relational part is a Relational Database Manager System ( A database that is ) there are other types of databases but the most popular is relational ( you know tables, columns, pk fk etc Oracle MySQL, MS-SQL, Postgresql )(*4)

And finally the Mapping part is where you do a bridge between your objects and your tables(*5)

  • Java: Hibernate ORM
  • Ruby: Active Record
  • PHP : Propel or Doctrine
  • Python : the Django ORM or SQLAlchemy

Active Record

The active record pattern is an approach to accessing data in a database. A database table or view is wrapped into a class. Thus, an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save. Any object loaded gets its information from the database. When an object is updated, the corresponding row in the table is also updated. The wrapper class implements accessor methods or properties for each column in the table or view.(*6)

So if I want to get an array containing a listing of all the users, instead of writing code to initiate a connection to the database, then doing some sort of SELECT * FROM users query, and converting those results into an array, I can just type User.all and Active Record gives me that array filled with User objects that I can play with as I’d like.(*7)

Even more impressive, it doesn’t really matter which type of database you’re using (as long as you’ve set up the config/database.yml file properly), Active Record smooths out all the differences between those databases for you so you don’t have to think about it. You focus on writing code for your application, and Active Record will think about the nitty gritty details of connecting you to your database. It also means that if you switch from one database to another, you don’t actually need to change any major application code, just some configuration files.(*8)

That’s a step ahead of ourselves, though, because first it makes sense to think about what the relationship is between Rails and a database anyway. It’s actually pretty straightforward — you want to store information about your users, so you create a database table called users. You want to be able to access that data from your application, so you create a model called User, which is really just a Ruby file which inherits from Active Record and thus gets to use all the handy methods I was alluding to earlier like all and find and create. One table corresponds with one model which inherits from Active Record.(*9)

Let’s take this example(*10):

We have Collage class and inherits from Active Record which is the way how c we can implement Active Record to Class object.

class College < ActiveRecord::Base
has_many :students
end
class Student < ActiveRecord::Base
belongs_to :college
end

in our migration we add a foreign key for referencing another table:

class CreateColleges < ActiveRecord::Migration
def change
create_table :colleges do |t|
t.string :name
t.timestamps
end
end
end
class CreateStudents < ActiveRecord::Migration
def change
create_table :students do |t|
t.string :name
t.integer :college_id
t.timestamps
end
end
end
  1. Student and College are classes and there are corresponding table in the database.
  2. Objects of the class Student and College correspond to rows in the table.
  3. Attributes of an Student and College such as name correspond to columns from the row.
2.2.2 :003 > college = College.find(17)
College Load (0.5ms) SELECT "colleges".* FROM "colleges" WHERE "colleges"."id" = $1 LIMIT 1 [["id", 17]]
=> #
2.2.2 :004 > college.students
Student Load (0.7ms) SELECT "students".* FROM "students" WHERE "students"."college_id" = $1 [["college_id", 17]]
=> #
2.2.2 :005 >

Observe that college.students are converted into SQL queries, to get all students having college_id as '17'.

Student Load (0.7ms)  SELECT "students".* FROM "students" WHERE  "students"."college_id" = $1  [["college_id", 17]]

This is done using ActiveRecord and has_many , belongs_to , has_one etc., are it's methods for which corresponding SQL queries are fired.

Conclusion

With ORM, we don’t have to type, SQL format data, for example,

UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition;

or

CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
….
);

instead when we inherit ActiveRecord::Base and use has_many, has_one relationsips.

Resources

consept *6 is taken form above website.

consept *1, is taken from above website.

conspet *10 is taken form above website.

consept *7,*8,*9 is taken from above website.

consept *2,*3, *4, *5 is taken form; this above website.

https://viblo.asia/p/object-relational-mapping-ruby-active-record-KE7bGoAOR5e2

--

--

Yahya Gok
Yahya Gok

No responses yet