The database is a huge 'closet'.

Database is simply 'accurately organized drawer'. Try to understand the database through the experience of storing and retrieving memories with Rails.

밤치 33

Databases are giant 'cabinets': Storing and retrieving memories with Rails

When we first create a web service, there is one concept that is most confusing.

"Where exactly is the information I entered... stored?"

To understand a database, you don't need grand concepts.
A database is simply a 'neatly organized cabinet'.

  • The cabinet is a table

  • Each drawer is a record

  • The compartments inside the drawer are columns

  • The types of cabinets (Post, User, etc.) are models

  • The act of opening and closing the cabinet is a query

Once you grasp this analogy,
the database is no longer a difficult entity
but a very physical concept that can be understood at your fingertips.

Now let's create it directly with Rails.


Part 1. Creating a 'cabinet (Post table)'

In the terminal:

rails generate model Post title:string body:text
rails db:migrate

This command is equivalent to saying:

"I'm going to create a cabinet named Post.

Inside the cabinet, shall we create two compartments?

One for the title,

and one for the body."

Rails takes this command and installs a new cabinet (Post table) in the big room called the database.

Each drawer (record) in that cabinet
will always have two fixed compartments: title and body.

Now we are ready to save.


Part 2. Creating a 'drawer (record)' and putting it in the cabinet

Let's enter the Rails console.

rails console

And with the following code,
we create a drawer (one Post) and put it in the cabinet.

Post.create(title: "First post", body: "Stored in the DB cabinet!")

What happens here:

  • Inside the Post cabinet,

  • in the title compartment is "First post",

  • in the body compartment is "Stored in the DB cabinet!",

    a new drawer (record) is added.

This drawer will never disappear now.
Even if you turn off and on the server, it will remain as it is.

This is why the database is "the memory of the web".


Part 3. 'Opening the cabinet':

Let's retrieve the stored drawers.

Post.all

Rails tells the database like this:

"Post cabinet, please bring out all the drawers in there."

Then the database shows us exactly the drawers we put in.

This is the power of 'a space that never forgets'.


Part 4. 'Intelligent cabinet that automatically organizes the structure'

Let's retrieve one with Post.first and check it.

post = Post.first
post.id
post.created_at
post.updated_at

When putting drawers in, the database automatically:

  • assigns an id (drawer number),

  • created_at (when it was put in),

  • updated_at (when it was modified),

It organizes and stores such meta information.

This is not just a cabinet,
but a smartly organized cabinet.


Part 5. 'People don't need to search through thousands of drawers'

The database finds it immediately like a super-fast librarian

For example,
if you only want to find the drawer with the title "First post":

Post.where(title: "First post")

With this one line,
even among thousands of drawers,
the database will pull out the exact drawer.

It's like a librarian instantly grasping
the drawer number, location, and attributes.

Because of this fast search function,
the database is responsible for the speed and accuracy of the web service.


Part 6. 'Intellectual network connecting drawers'

Let's connect Comments to the Post cabinet

Enter the following command.

rails generate model Comment body:text post:references
rails db:migrate

This can be interpreted as follows:

"Create a cabinet (Comment) to store comments.

Inside the cabinet, there is a compartment called 'body',

and remember which Post cabinet this drawer (Comment) belongs to."

Rails automatically creates a compartment called post_id in the Comment cabinet
and stores the connection information as "This drawer belongs to that Post drawer."

It's like putting a sticker on the side of the drawer
saying "This drawer belongs to Post #1".


Part 7. Let Rails know about the relationships

app/models/post.rb

class Post < ApplicationRecord
  has_many :comments
end

app/models/comment.rb

class Comment < ApplicationRecord
  belongs_to :post
end

Telling Rails like this:

"The Post cabinet can hold multiple Comment drawers."

"A Comment drawer must belong to a Post drawer."

Now the database knows
the relationships between the drawers.

This structure is
the intellectual network that makes up the web service.


Part 8. Let's actually connect the data

In the console:

post = Post.first
post.comments.create(body: "This post is interesting!")
post.comments.create(body: "Second comment!")

Then let's retrieve the comments.

post.comments

The output contains
the two comments we just created.

Looking at this, you realize.

  • The database doesn't just store

  • but connects different pieces of information

  • to create a single intellectual network.

Because of this connectivity,
the web service operates like an "organism."


Part 9. Now you understand why the database is

the 'heart of the web'

By following along until now,
you have accurately experienced the essence of the database.

The memory of the web

→ Storing and retrieving data.

A space that never forgets

→ Even after a restart, Posts and Comments remain.

Neatly organized cabinet

→ Structured compartments, automatically organized meta information (id, timestamps).

A librarian who can find millions instantly

→ Searchable immediately with conditions like where.

An intellectual network of connected drawers

→ Establishing Post ↔ Comment relationships.

The heart of the web

→ Storage + retrieval + relationships = all web functions.

All of this,
you can now create and handle it yourself.


And in the reader's mind, the following thoughts begin to sprout

"Wait... am I really someone who can create web services?"

"I could make my own blog, couldn't I?"

"Should I try making a TODO service to share with friends?"

"I understand how the data I input is stored, connected, and retrieved!"

Once you pass this moment of awakening,
the web is no longer a "world to use."
The web becomes a world that you create.

Comments

Add a Comment

Sign in to comment