What is SQL? And why don't we use SQL much in Rails? - The existence of ActiveRecord
Many novice developers feel confused at this point.
Don't we need SQL when using a database?
But why does a web service work in Rails without using SQL much?
What exactly does ActiveRecord do to make this possible?
This article clearly explains the core of that.
1⃣ What is SQL? (Essential summary only)
SQL(Structured Query Language) is
the official language for interacting with databases.
When requesting operations to the DB, SQL is used.
Example:
SELECT * FROM users WHERE age > 20;
This statement means
"Retrieve records from the users table where the age is greater than 20".
The things you can do with SQL are neatly summarized into four:
Create - creating data
Read - reading data
Update - modifying data
Delete - deleting data
In short, CRUD.
Ultimately, all web services
request CRUD to the DB through SQL.
2⃣ However, SQL is rarely used in Rails. Why? Because of ActiveRecord.
One of the core components of Rails is ActiveRecord.
ActiveRecord is an ORM(Object Relational Mapping).
What does this mean?
3⃣ What is ORM? A system that automatically connects "objects ↔ databases(tables)"
Programming languages use objects.
user = User.new(name: "bamcoding")
Databases use records.
1st row of the users table
ORM has one role:
To automatically connect "Ruby world objects" with "DB world records".
This allows us to
handle databases like objects.
4⃣ Replacing SQL with ActiveRecord
Example 1) Reading data(Read)
SQL:
SELECT * FROM users;
Rails:
User.all
Example 2) Conditional search
SQL:
SELECT * FROM users WHERE age > 20;
Rails:
User.where("age > 20")
Or more Ruby-like:
User.where(age: 21..)
Example 3) Creation(Create)
SQL:
INSERT INTO users (name, age) VALUES ('bamcoding', 34);
Rails:
User.create(name: "bamcoding", age: 34)
Example 4) Update
SQL:
UPDATE users SET age = 35 WHERE id = 1;
Rails:
user = User.find(1)
user.update(age: 35)
Example 5) Deletion(Delete)
SQL:
DELETE FROM users WHERE id = 1;
Rails:
user = User.find(1)
user.destroy
5⃣ Key: Rails developers handle DB with "Ruby", not SQL
Because ActiveRecord
translates the Ruby commands I give
into SQL in the background.
In other words,
Even if developers don't use SQL, Rails generates and executes SQL internally.
Thanks to this, developers
can focus on designing business logic
without the technical burden of directly controlling the DB.
6⃣ The real reason why ActiveRecord is said to be "powerful"
The strength of ActiveRecord is not just "not needing to use SQL".
That's just a superficial advantage.
The real core consists of the following four points.
1) Models and DB tables are automatically connected
Creating a User model
automatically connects it with the users table.
No need to match table names
No need to specify mappings
Rails automatically connects by convention
This significantly boosts development speed.
2) Relationship setup is overwhelmingly intuitive
Example:
class Building < ApplicationRecord
has_many :rooms
end
class Room < ApplicationRecord
belongs_to :building
end
These two lines
make Rails do the following by default:
Create appropriate SQL JOINs
Support building.rooms calls
Manage the automatic foreign key building_id
Optimize relationship-based queries
To implement this directly in SQL,
you would need dozens of JOINs, INDEXes, and conditions.
ActiveRecord accomplishes all of this
in just two lines.
3) Security, injection prevention, and query optimization are handled by Rails
Risks involved in directly using SQL:
SQL Injection
Performance degradation due to incorrect queries
Overuse of SELECT *
Slow queries due to missing INDEXes
ActiveRecord
prevents most of these issues in advance
or converts them into safe SQL.
It's a safety net for web developers.
4) Fully integrated development experience with Ruby syntax
You can assemble queries in a Ruby-like manner:
User.where(active: true)
.where("age > ?", 20)
.order(created_at: :desc)
.limit(10)
It looks like Ruby code,
but internally, optimized SQL is generated.
This ability to "combine code"
makes ActiveRecord one of the most powerful ORMs.
7⃣ Conclusion - Why Rails developers don't use SQL much
ActiveRecord abstracts all the complexity of SQL
Developers only need to write Ruby code
Model-relationship-query-save-delete all feel like manipulating Ruby objects
Productivity, safety, and maintainability are significantly improved
ActiveRecord is not just a simple ORM,
it is the core engine of Rails productivity.