I used it without knowing the O / R mapping of rails, so I checked it.

First of all, what is O / R mapping?

Object / relational mapping (sometimes abbreviated as O / R mapping or ORM) is the connection of rich objects in an application to a relational database (RDBMS) table. With ORM, you can save and read object attributes and relationships in your application from the database by writing a small amount of access code instead of writing SQL statements directly. Rails Guide

In short, if you want to get ʻusers` information when using SQL,

SELECT * FROM users ;

You should get all the records with, but if you use O / R mapping

user.all

Like, in rails, thanks to ActiveRecord, it is possible to get all the contents of users without writing SQL.

I often use it

.find_by(name: 'TARO')

Methods like

SELECT * FROM users WHERE name = "TARO";

You will get the same result as SQL like this.

As mentioned above, it is possible to reduce the work of complicated database operations such as the processing that originally wrote SQL.

While it has the advantage of being convenient, it also has some disadvantages.

1. There is no loss in knowing CRUD processing is applied without knowing SQL.
2. Slow, memory consumption is high.
3. More difficult than SQL when complex processing is required.

There are other disadvantages such as not being able to use the functions of a specific DB, but there are also advantages such as being able to search data intuitively and not having to modify the source code when changing the database.

Since I was studying with almost no study of SQL myself, there were many situations where knowledge of SQL was required when proceeding with development with java and kotlin in business, and at first I was confused at all.

Recently, it is often installed as standard in web frameworks, and it is necessary to know the syntax for each language, but I would like to actively use the O / R mapper as long as it can be supported. I have.

reference

https://qiita.com/gomiryo/items/6d448c500749f91242d2 https://railsguides.jp/active_record_basics.html

Recommended Posts

I used it without knowing the O / R mapping of rails, so I checked it.
Step-by-step understanding of O / R mapping
I checked the place of concern of java.net.URL # getPath
The code I used to connect Rails 3 to PostgreSQL 10
I checked the number of taxis with Ruby
I got an "ActionView :: Template :: Error: Permission denied" error in the test of 3.3.1 of Rails tutorial 6th edition, so I solved it.
A review of the code used by rails beginners
List of things I used without understanding well: Ruby
I summarized the display format of the JSON response of Rails
I introduced Docker to Rails 6, so I summarized it (beginner)
I can't get out of the Rails dbconsole screen
When I used Slick on Rails, it competed with Turbolinks.
[Action View :: Missing Template] I didn't understand the meaning of the error statement, so I looked it up.
I did Docker construction of rails + postgresql as official, but it didn't work, so I fixed some
I didn't understand the meaning of injection such as DI or @Autowired, so I looked it up.