# 20210217(수)

## 2021-02-17

### 1. 학습 날짜

* 2021-02-17

### 2. 학습 시간

* 12:00 \~ 23:00

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

* 백본 view, model, collection
* 루비온레일즈

### 4. 학습 목표

* 레일즈 DB 학습&#x20;

### 5. 학습 정리

* x

### 6. 상세 학습 내용

* 레일즈의 DB관련해서 문제가 생겼다.
* 동시 다발적으로 여러 유저가 게임시작을 하면, 하나의 게임에 여러 유저가 들어오는 문제가 발생했다.
* 이 문제는 DB에 lock이 걸려있지 않아서 생기는 문제였다.
* 따라서 트랜잭션을 이용해서 DB을 보호하려고 시도했다.
* 하지만 트랜잭션은 여러 쿼리를 모았다가 한번에 처리하는 기능이지 막는 기능은 아니다.
* 따라서 트랜잭션으로 여러 쿼리를 모았다가, 한번에 처리하면서 특정 모델에는 lock 을 걸어서 동시 접근을 막으면 된다.
* 트랜잭션을 사용하는 방법은 아래와같다.

  ```
  Game.transaction do
  ...
  end
  ```
* 중복으로 트랜잭션을 사용하려면 아래처럼 하자

  ```
  Game.transaction do
  Rule.transaction do
  ...
  end
  ...
  end
  ```
* 위처럼 동작 하긴 하지만 안좋은 방법 아래가 좋은 방법

```
ActionRecord::Base.transaction do
...
end
```

* 이제 여기서 락을 걸려면 아래처럼 하자

```
ActionRecord::Base.transaction do
  @matches.with_lock do
  ...
  end

end
```

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

* 빨리 끝내고 싶다

### 8. 다음 학습 계획

* guild index 뷰 만들기


---

# 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/february/20210217.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.
