We have just explained the database (DB)
using metaphors such as library, memory, librarian, and network.
Now, let's actually see it in action.
Let's see
how a web service stores information,
how it remembers,
how it retrieves and displays,
and how it connects with each other.
It's time to try it out directly with Rails
and feel it with your own body.
Part 1. Creating the "Memory of a Web Service"
Let's create a Post model
The memory of the web starts with recording.
Let's call the record a "post."
In the terminal:
rails generate model Post title:string body:text
rails db:migrate
What these two lines say is very simple.
"We will create a structure called Post with memory spaces called title and body."
"And we will reflect that structure in the actual memory warehouse called DB."
Now Rails has created a "shelf (Post table)" in the library,
and it is now possible to place a two-space book titled "title, body" on that shelf.
Part 2. Interacting with the DB Directly Using Rails Console
Let's put a book on the library shelf
In the terminal:
rails console
And let's put in one book.
Post.create(title: "My First Post", body: "Saved in Rails DB!")
The moment you press Enter,
you have recorded the first data in the "memory" of the web service.
This one line is much more powerful
than any code you have written so far.
Because this is
creating memory that does not disappear.
Part 3. The DB is a "Space that Never Forgets"
Let's ask again in the Rails console.
Post.all
Then Rails says to the DB like this.
"Bring me all the books on the Post shelf!"
And the DB retrieves the data you just saved
without a single error.
This is
the memory of the web and a space that never forgets.
Even if you turn off the server,
restart the computer,
or come back a week later,
that memory remains intact.
Part 4. Experience the "Well-Organized Library"
ID, created_at, updated_at are the library's classification system
Let's check the Post we just created again.
post = Post.first
post.id
post.created_at
post.updated_at
Now you know.
id = book number
created_at = date the book was created
updated_at = date the book was last modified
The DB
meticulously organizes all records
even without you doing anything.
These records
form the basis for structuring and storing vast amounts of data.
The DB is not just a simple space
but a well-organized library.
Part 5. Experience the "Librarian Who Quickly Finds Millions of Books"
Let's find a book by condition
Post.where(title: "My First Post")
Rails tells the DB.
"Find only the books with the title 'My First Post'."
The DB quickly finds and displays them in an instant.
Whether it's 10, 1,000,
or 1 million data,
it finds them accurately.
Because the DB
has superhuman librarians who can immediately find books
even in a vast library.
You have just
used that ability firsthand.
Part 6. Create a "Connected Intellectual Network" Yourself
Let's create Comments and connect them to Post
The moment a web service truly becomes powerful
is when data starts to be connected.
For example,
let's allow comments to be added to blog posts.
rails generate model Comment body:text post:references
rails db:migrate
This model says the following.
"Comments have content (body) and"
"will store data belonging to a specific post (post_id)"
Let's inform Rails of the relationship
app/models/post.rb
class Post < ApplicationRecord
has_many :comments
end
app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :post
end
Now Post and Comment
become a perfectly connected intellectual network.
Part 7. Let's Create Actually Connected Data
In the console:
post = Post.first
post.comments.create(body: "First comment!")
post.comments.create(body: "Second comment!")
Now let's read the comments.
post.comments
The DB knows exactly.
which comment belongs to which Post
how many comments are attached to a post
when those comments were saved
The moment data is connected,
the web service finally becomes an organic system.
Part 8. Now You Understand Why DB is the "Heart" of a Web Service
In the short practice you have followed so far,
you have experienced the following.
Memory of a Web Service
→ Process of storing and retrieving data
Space that Never Forgets
→ Data created with Post.create is permanently maintained
Well-Organized Library
→ Features like id, timestamps for automatic organization
Librarian Who Quickly Finds Millions
→ Ability to search for desired data with Post.where
Intellectual Network
→ Establishing Post ↔ Comment relationships to create data connections
Heart of a Web Service
→ Memory + Relationships + Search all take place in DB
Now,
you understand why a web service cannot exist without a database
and that handling DB is essentially handling a web service
at your fingertips.
A Thought Blooms in the Reader's Mind
Readers who have followed this far will surely feel like this.
"Wait... I could probably
create my own blog with this level of knowledge, right?""I might be able to make a TODO app to share with friends, too."
"Web services... seem more doable than I thought?"
This is the moment of awakening we aimed for.
The moment data is in your hands,
a web service is no longer a technology of the distant future
but a reality being created in your hands right now.
And Rails whispers to you like this.
"Now decide what service to create.
I'll help you with the rest."