Chapter 13 - nortonbino/sample_app GitHub Wiki

Chapter 13

13.1.1

Ex 1)

 created_at: nil, updated_at: nil> 

Ex 2)

2.3.1 :008 > micro.user
 => #<User id: 1, name: "Example User", email: "[email protected]", created_at: "2017-10-24 13:50:10", updated_at: "2017-10-24 13:50:10", password_digest: "$2a$10$m5O2qM51qiY1Hx4ZAiE6WOGo2ST9Jplkcz7K1fO9VJh...", remember_digest: nil, admin: true, activation_digest: "$2a$10$mq9XH2aKQNUzUzfPFotlbOYqYCkgUk9BL.DerHzXEA6...", activated: true, activated_at: "2017-10-24 13:50:10", reset_digest: nil, reset_sent_at: nil> 
2.3.1 :009 > micro.user.name
 => "Example User" 

Ex 3)

["created_at", "2017-11-07 11:43:02.135353"], ["updated_at", "2017-11-07 11:43:02.135353"]]
```.

## 13.1.2)

### Ex 1) It's not valid.

## 13.1.3

### Ex 1)

2.3.1 :002 > micropost = user.microposts.create(content: "Lorem ipsum") (0.1ms) begin transaction SQL (0.8ms) INSERT INTO "microposts" ("content", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) ["content", "Lorem ipsum"], ["user_id", 1], ["created_at", "2017-11-07 12:20:05.909382"], "updated_at", "2017-11-07 12:20:05.909382" (101.5ms) commit transaction => #<Micropost id: 2, content: "Lorem ipsum", user_id: 1, created_at: "2017-11-07 12:20:05", updated_at: "2017-11-07 12:20:05">


### Ex 2)

2.3.1 :004 > user.microposts.find(micropost) ArgumentError: You are passing an instance of ActiveRecord::Base to find. Please pass the id of the object by calling .id. from (irb):4


### Ex 3) True. False.

## 13.1.4

### EX 1) Differ in minutes.

### Ex 2) 

SELECT "microposts".* FROM "microposts" ORDER BY "microposts"."created_at" DESC LIMIT ? "LIMIT", 1

SELECT "microposts".* FROM "microposts" ORDER BY "microposts"."created_at" ASC LIMIT ? "LIMIT", 1


### Ex 3)

2.3.1 :006 > user.destroy (0.2ms) begin transaction Micropost Load (0.4ms) SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? ORDER BY "microposts"."created_at" DESC "user_id", 1 SQL (0.3ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 295 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 289 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 283 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 277 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 271 SQL (0.3ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 265 SQL (0.6ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 259 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 253 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 247 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 241 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 235 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 229 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 223 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 217 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 211 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 205 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 199 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 193 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 187 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 181 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 175 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 169 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 163 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 157 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 151 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 145 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 139 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 133 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 127 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 121 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 115 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 109 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 103 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 97 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 91 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 85 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 79 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 73 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 67 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 61 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 55 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 49 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 43 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 37 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 31 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 25 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 19 SQL (0.2ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 13 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 7 SQL (0.1ms) DELETE FROM "microposts" WHERE "microposts"."id" = ? "id", 1 SQL (0.3ms) DELETE FROM "users" WHERE "users"."id" = ? "id", 1



## 13.2.2

### Ex 1)

2.3.1 :009 > (1..10).to_a.take(6) => [1, 2, 3, 4, 5, 6]


### Ex 2) Only to create an array, but you can still get the values using (1..10).take(6)



## 13.3.1

### Ex 1) because it's not responsibility of the User controller to know that.

## 13.3.2

###