DEV Community

Cover image for ORM Explained
Miriamfark
Miriamfark

Posted on

ORM Explained

Hi All!

I am back with another blog post, this time to tell you all about ORM's - Object-Relational Mapping.

Object - Relational Mappers are a developers dream. Every app will need a database to persist information and allow full, dynamic CRUD abilities. Speaking to a database involves learning a database language, usually SQL. This is what a SQL request looks like:

$SELECT * FROM table_name;

A more complicated SQL request including a condition may look like this:

$SELECT * FROM table_name WHERE id = 1

However, ORMs change that by being the intermediate layer, the go between, for the database and your preferred programming language.

How Do ORMs Work?

Object-Relational Mappers work by establishing a link between object-oriented programming classes and database tables. Each row in a table is represented by an instance of the linked class.

What an ORM is really doing is "wrapping" the SQL statements that will talk to the database in code, to automate the SQL requests, and avoid repetition. This makes sense according to programmers #1 rule: Keep it DRY! (Don't repeat yourself!) it is similar to creating a method that performs a task and calling that method multiple times. It cuts down on annoying, repetitive code, and you can use a language that is more familiar.

For example, if you are building a to-do app (check out mine!) you will need a table of tasks. Every time you want to add a new task to the table, you will have to write a SQL statement such as:

$ INSERT INTO tasks (name, category, minutes) VALUES ('Sweeping', 'Housework', 20)

Instead, an ORM will abstract away that logic and you can more easily say:

$ Task.create(name:'Sweeping, category:'Housework', minutes:20)

This is much more familiar, much cleaner, and much quicker!

ORM Conventions

Following ORM conventions is very important, as many of the functions depend on the conventions being followed. If you don't follow the conventions, your ORM will not work!

  1. Tables are lower case and plural. Example: tasks, days, categories
  2. Classes are upper case and singular. Example: Task, Day, Category

It is important to remember that every table is linked to a class and every instance is linked with a row in a table. However, the row is not the object and the object is not the row. The object is merely a representation of what actually exists within the database.

Common ORM Capabilities

Most ORMs, such as Active Record, have amazing capabilities. With very little code you can get access to very useful methods. Here are methods for Create, Read, Update and Delete.

1. Create

$ Day.create(name: 'Sunday')

or you can instantiate a new day without saving, or save it in a separate command.

sunday = Day.new
sunday.name = 'Sunday'
sunday.save
Enter fullscreen mode Exit fullscreen mode

2. Read

There are several ways of reading data, depending on information you require. If you would like all the rows of a specific table you would say:

$ Day.all

for just the first day in a collection you can say

$ Day.first

you can search by name, id, or other attribute

$ Day.find_by(name: 'Sunday)

3. Update

To update an attribute, you will usually have to first find the attribute by searching for and then updating.

monday = Day.find_by(name: 'Monday')
monday.update(name: 'Meatless Monday')
```

`

####4. Delete
Likewise, for delete you will first have to find the object and then delete it.

`

```
monday = Day.find_by(name: 'Monday')
monday.destroy
```

`

## For More Information
https://guides.rubyonrails.org/active_record_basics.html
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jhelberg profile image
Joost Helberg

The 4. Item is a great example of why an ORM doesn't satisfy any developer in the end. Finding and deleting is not the pattern to use when destroying Monday's. The plain sql (1 statement) does the correct thing, the 2 orm-statements do not and, at best, is twice as slow.