Skip to content

Commit 5d2fb89

Browse files
committed
+ part: active record
1 parent 866c65d commit 5d2fb89

File tree

35 files changed

+445
-7
lines changed

35 files changed

+445
-7
lines changed

src/content/tutorial/4-database/2-database-migrations/content.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
type: lesson
33
title: Database Migrations
4+
focus: /workspace/store/db/migrate/20250521010850_create_products.rb
45
custom:
56
shell:
67
workdir: "/workspace/store"
@@ -21,7 +22,7 @@ they can be deployed to production (live, online!) safely.
2122

2223
In the editor, open the migration Rails created for us so we can see what
2324
the migration does. This is located in
24-
`db/migrate/<timestamp>_create_products.rb`:
25+
`db/migrate/20250521010850_create_products`:
2526

2627
```file:/workspace/store/db/migrate/20250521010850_create_products.rb
2728
```

src/content/tutorial/4-database/3-running-migrations/content.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
type: lesson
33
title: Running Migrations
4+
focus: /workspace/store/db/migrate/20250521010850_create_products.rb
45
custom:
56
shell:
67
workdir: "/workspace/store"
@@ -23,10 +24,10 @@ This command checks for any new migrations and applies them to your database.
2324
Its output looks like this:
2425

2526
```bash
26-
== 20240426151900 CreateProducts: migrating ===================================
27+
== 20250521010850 CreateProducts: migrating ===================================
2728
-- create_table(:products)
2829
-> 0.0030s
29-
== 20240426151900 CreateProducts: migrated (0.0031s) ==========================
30+
== 20250521010850 CreateProducts: migrated (0.0031s) ==========================
3031
```
3132

3233
:::tip
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": "../../../4-database/3-running-migrations/_files"
2+
"extends": "../../../../../templates/create-products"
33
}

src/content/tutorial/5-console/1-rails-console/content.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ title: Rails Console
44
custom:
55
shell:
66
workdir: "/workspace/store"
7-
fs:
8-
remove:
9-
- "/workspace/store"
107
---
118

129
Rails Console
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../../../templates/create-products"
3+
}

src/content/tutorial/6-active-record/1-intro/_files/workspace/.keep

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
type: lesson
3+
title: Active Record Basics
4+
focus: /workspace/store/app/models/product.rb
5+
custom:
6+
shell:
7+
workdir: "/workspace/store"
8+
---
9+
10+
Active Record Basics
11+
--------------------------
12+
13+
When we ran the Rails model generator to create the `Product` model, it created
14+
a file at `app/models/product.rb`. This file creates a class that uses Active
15+
Record for interacting with our `products` database table.
16+
17+
```ruby
18+
class Product < ApplicationRecord
19+
end
20+
```
21+
22+
You might be surprised that there is no code in this class. How does Rails know
23+
what defines this model?
24+
25+
When the `Product` model is used, Rails will query the database table for the
26+
column names and types and automatically generate code for these attributes.
27+
Rails saves us from writing this boilerplate code and instead takes care of it
28+
for us behind the scenes so we can focus on our application logic instead.
29+
30+
Let's use the Rails console to see what columns Rails detects for the Product
31+
model.
32+
33+
Run:
34+
35+
```irb
36+
store(dev)> Product.column_names
37+
```
38+
39+
And you should see:
40+
41+
```irb
42+
=> ["id", "name", "created_at", "updated_at"]
43+
```
44+
45+
Rails asked the database for column information above and used that information
46+
to define attributes on the `Product` class dynamically so you don't have to
47+
manually define each of them. This is one example of how Rails makes development
48+
a breeze.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../../../templates/create-products"
3+
}

src/content/tutorial/6-active-record/2-creating-records/_files/workspace/.keep

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
type: lesson
3+
title: Creating Records
4+
focus: /workspace/store/app/models/product.rb
5+
custom:
6+
shell:
7+
workdir: "/workspace/store"
8+
---
9+
10+
### Creating Records
11+
12+
We can instantiate a new Product record with the following code:
13+
14+
```irb
15+
store(dev)> product = Product.new(name: "T-Shirt")
16+
=> #<Product:0x000000012e616c30 id: nil, name: "T-Shirt", created_at: nil, updated_at: nil>
17+
```
18+
19+
The `product` variable is an instance of `Product`. It has not been saved to the
20+
database, and so does not have an ID, created_at, or updated_at timestamps.
21+
22+
We can call `save` to write the record to the database.
23+
24+
```irb
25+
store(dev)> product.save
26+
TRANSACTION (0.1ms) BEGIN immediate TRANSACTION /*application='Store'*/
27+
Product Create (0.9ms) INSERT INTO "products" ("name", "created_at", "updated_at") VALUES ('T-Shirt', '2024-11-09 16:35:01.117836', '2024-11-09 16:35:01.117836') RETURNING "id" /*application='Store'*/
28+
TRANSACTION (0.9ms) COMMIT TRANSACTION /*application='Store'*/
29+
=> true
30+
```
31+
32+
When `save` is called, Rails takes the attributes in memory and generates an
33+
`INSERT` SQL query to insert this record into the database.
34+
35+
Rails also updates the object in memory with the database record `id` along with
36+
the `created_at` and `updated_at` timestamps. We can see that by printing out
37+
the `product` variable.
38+
39+
```irb
40+
store(dev)> product
41+
=> #<Product:0x00000001221f6260 id: 1, name: "T-Shirt", created_at: "2024-11-09 16:35:01.117836000 +0000", updated_at: "2024-11-09 16:35:01.117836000 +0000">
42+
```
43+
44+
Similar to `save`, we can use `create` to instantiate and save an Active Record
45+
object in a single call.
46+
47+
```irb
48+
store(dev)> Product.create(name: "Pants")
49+
TRANSACTION (0.1ms) BEGIN immediate TRANSACTION /*application='Store'*/
50+
Product Create (0.4ms) INSERT INTO "products" ("name", "created_at", "updated_at") VALUES ('Pants', '2024-11-09 16:36:01.856751', '2024-11-09 16:36:01.856751') RETURNING "id" /*application='Store'*/
51+
TRANSACTION (0.1ms) COMMIT TRANSACTION /*application='Store'*/
52+
=> #<Product:0x0000000120485c80 id: 2, name: "Pants", created_at: "2024-11-09 16:36:01.856751000 +0000", updated_at: "2024-11-09 16:36:01.856751000 +0000">
53+
```

0 commit comments

Comments
 (0)