# 20210101(금)

## 2020-01-01

### 1. 학습 날짜

* 2020-01-01

### 2. 학습 시간

* 11:00 \~ 14:00, 20:00 \~ 21:30

### 3. 학습 범위 및 주제

* 루비온레일즈 간단한 사이트 만들어보기

### 4. 학습 목표

* 댓글 기능 추가하기

### 5. 과제 제출

* x

### 6. 상세 학습 내용

1. 부모를 delete 할 때 모든 has\_many를 delete :white\_check\_mark:
   * [참고](https://stackoverflow.com/questions/2203835/how-can-i-delete-child-objects-when-the-parent-is-deleted-in-rails)
   * ```ruby
     has_many :reviews, :dependent => :destroy
     ```
2. 삭제할 때 `confimation` 이 나오게 만들기 :white\_check\_mark:
   * ```ruby
     <%= link_to 'Delete', post_path(post), data: {confirm: "정말로 삭제?"}, method: :delete  %>
     ```
   * 위 처럼 `data: {confirm: message}` 를 추가시켜주자.
3. 리뷰를 만들자. :white\_check\_mark:
   * 리뷰는 post에 달리므로 라우팅을 아래처럼 하자
   * ```ruby
     resources posts do
       resources reviews, shallow: true
     end
     ```
     * 라우팅 주소의 간소화를 위해 `shallow` 옵션을 추가함.
   * 리뷰는 기본적으로 로그인을 한 상태에서만 만들수있게하자. :white\_check\_mark:
4. 유저 프로필에서 자기 자신 삭제하는 기능 추가하기 :white\_check\_mark:
5. 포스트의 페이지네이션 기능 만들기
6. 유저 프로필에서 리뷰 볼수있게 만들기 :white\_check\_mark:
7. 리뷰 카운트 기능 만들기 :white\_check\_mark:
   * 처음 모델을 만들 때 `count` 필드를 만들지 않았었다. 따라서 카운트를 하는 필드를 추가해줘야만 했다.
   * 속성 하나만 추가할 때도 `rails` 명령어를 사용하면 된다.
   * 구체적으로 `post` 마이그레이션 파일에 `reviews_count:integer`를 하기위해서는 아래처럼 하면된다.

     ```
     rails generate AddReviewsCountToPosts
     ```
   * 이후에는 `xxxx_create_posts.rb` 파일에가서
     * `t.integer :reviews_count, default: 0` 을 추가해주자
8. user를 삭제하는데 `SQLite3::ConstraintException: FOREIGN KEY constraint failed` 예외가 계속 발생 해결 :white\_check\_mark:
   * 이 예외는 `user`를 삭제하는데 user 모델이 `has_many`를 하고 있는 `post`와 `review`의 의존관계를 명확히 하지 않아서 생기는 문제
   * 내가 의도한 바는 `user`를 삭제하면 자동으로 `post`와 `review` 모두 삭제 하는것
     * 이렇게 하기 위해서는 아래와 같은 작업을 해줘야 한다.

       ```ruby
       class User    < ApplicationRecord
         has_many :posts, :dependent => :destroy
         has_many :reviews, :dependent => :destroy
         ...
         end
       ```
   * 이전에는 `reviews` 에는 `dependent` 옵션을 사용하지 않았었다.
9. 한가지 생각할거리. `reviews`라는건 `post`에도 `has_many`로 되어있는거니깐 `user`에서는 굳이 `has_many reviews`를 할 필요 없지 않을까?
   * `user`에 `has_many :reviews`를 하지 않아도 확인할 수 있는 방법은 존재한다.
   * 다만 그렇게되면 `user`가 자신이 만든 댓글을 보기 위해서 서버에서 많은 리소스를 소모해야만한다.

### 7. 오늘 학습 내용에 대한 개인적인 총평

* 많이 앉아있었지만 오늘도 딴짓을 너무 많이했다.
* 그래도 내가 생각했던 아주 아주 아주 작은 로직을 구현한점은 칭찬할만하다.

### 8. 다음 학습 계획

* 레일즈 테스트, 클라이언트 개발 부분 책 읽기


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://simian114.gitbook.io/blog/undefined-1/diary/2021/january/20200101.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
