The Real Reason ActiveRecord is Cool - Because it 'abstracts' the DB, we can handle any DB
In programming, the most difficult problem is
dealing with different systems in a consistent manner.
Databases are especially like that.
MySQL has this syntax
PostgreSQL has that syntax
SQLite is different again
Oracle is a completely different world
To handle all of these directly,
developers need to remember everything from DB syntax, SQL dialect, indexing characteristics, type systems, to transaction processing methods every time.
However, Rails has solved all of these problems with just one layer called ActiveRecord.
This is exactly what abstraction is.
What is abstraction? (Concept itself without metaphor)
The essence of abstraction is simple.
Unifying complex systems into a single common interface (API).
In other words,
even though the operation methods are diverse,
making the usage methods uniform.
Abstraction is one of the most powerful techniques in the engineering world.
What did ActiveRecord abstract?
There is only one correct answer.
It unified the SQL syntax and behavior unique to each DB into Ruby object manipulation.
In other words,
It changed the "different world for each DB"
into "one world of Ruby code."
How powerful is this? Let's prove it with an example
For example, let's say we want to get a list of Users.
If you were to write SQL directly:
MySQL
SELECT * FROM `users` ORDER BY `created_at` DESC LIMIT 10;
PostgreSQL
SELECT * FROM users ORDER BY created_at DESC LIMIT 10;
Oracle
SELECT * FROM users WHERE ROWNUM <= 10 ORDER BY created_at DESC;
The syntax is all different.
LIMIT is different, query rules are different, reserved words are different.
But with ActiveRecord?
Rails
User.order(created_at: :desc).limit(10)
Done.
Based on this one Ruby code,
ActiveRecord automatically converts it into SQL tailored to each DB.
For MySQL, it's MySQL-specific SQL
For PostgreSQL, it's PostgreSQL-specific SQL
For SQLite, it's SQLite-specific SQL
In other words, developers only need to know Ruby.
Real value provided by abstraction
The benefits that ActiveRecord gains through abstraction are as follows.
1) Freedom to switch DBs
For example, let's say we switch from SQLite to PostgreSQL.
Normally:
Modify all SQL
Change all types
Modify all transaction processing
Modify all LIMIT/OFFSET syntax
But in Rails...
There is no need to change anything.
Just change the adapter in the Rails config.
adapter: postgresql
The code remains the same:
User.where(active: true)
And it works just fine.
2) You can develop without knowing about the DB
ActiveRecord handles the following automatically:
Data storage
Modification
Querying
Deletion
Transactions
Relationship setting
Foreign key handling
JOIN optimization
Developers only need to do this.
user = User.find(5)
user.update(name: "New Name")
But in reality, complex SQL is executed automatically behind the scenes in the DB:
UPDATE users SET name = 'New Name' WHERE id = 5;
Developers don't need to know this.
3) Enables Model-driven Thinking
SQL is data-centric thinking.
But Rails is object-centric thinking.
In other words, we handle the User model like a real object.
user.posts
This one line handles everything like JOIN, WHERE, INDEX, relationship interpretation... with ActiveRecord,
developers can focus on designing business logic.
The moment you understand abstraction, ActiveRecord's genius becomes apparent
Look at the following Ruby code.
users = User.where("age > ?", 18).order(:created_at)
When this code is executed, internally:
It looks at the model structure
Checks the type of DB
Safely escapes the WHERE condition
Applies the sorting method
Converts it into SQL syntax understood by the respective DB
Executes the SQL
Converts the returned value into a record as a Ruby object (User)
All of these processes do not need to be consciously done by the developer.
In other words,
ActiveRecord = A tool that completely assimilates the DB world into the Ruby world
Thanks to this abstraction,
Rails developers can handle all the data of the entire service
with just a few lines of Ruby code,
even without being a DB expert.
Conclusion - ActiveRecord is not an ORM but a 'DB abstraction engine'
Why is ActiveRecord a "really cool guy"?
There is only one reason.
ActiveRecord makes developers forget all the different complexities and rules of each DB.
And it allows you to handle all kinds of DBs in the same way with a single language, Ruby.
This is the power of abstraction.
This is the philosophy of Rails.
This is the answer to why ActiveRecord is a top-tier ORM.