# Custom\_fields를 동시에 업데이트하면 값이 배열로 변합니다

**URL:** https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034
**Category:** Development
**Tags:** rest-api
**Created:** [8월 25, 2020, 3:32오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034 "2020-08-25T15:32:44Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Stefan\_Brighiu\_M](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stefan_brighiu_m/32/174499_2.png) [@Stefan\_Brighiu\_M](https://meta.discourse.org/u/Stefan_Brighiu_M)
#### Post date: [8월 25, 2020, 3:32오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034/1 "2020-08-25T15:32:44Z")

</div>

이 게시글 이후로 꽤 오래전에 수정된 것 같습니다. [Custom\_fields simultaneous save with json becomes an array](https://meta.discourse.org/t/custom-fields-simultaneous-save-with-json-becomes-an-array/73647)

불행히도 현재 여러 엔드포인트가 동시에 호출되어 user custom\_fields를 동시에 업데이트하는 문제를 겪고 있습니다. 이로 인해 제가 :text로 설정한 값들이 배열로 변환되고 있습니다.

![3 endpoints called and setting custom_field to array instead of string](https://cdck-file-uploads-global.s3.dualstack.us-west-2.amazonaws.com/meta/original/3X/1/5/15c538a891de305b53e02a8e20004e824438d232.png)

도움 주실 수 있을까요? 현재 이 문제로 인해 제 프로젝트 전체가 막혀 있습니다…

로컬 개발 환경에서 2.5.0.beta7 버전에서 이 문제를 겪고 있습니다.

이런 상황에서 값이 저장될 때까지 DB에 잠금(lock)을 걸 수 있는 방법이 있을까요?

---

<div class="post-metadata">

### Author: ![Stefan\_Brighiu\_M](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stefan_brighiu_m/32/174499_2.png) [@Stefan\_Brighiu\_M](https://meta.discourse.org/u/Stefan_Brighiu_M)
#### Post date: [8월 25, 2020, 3:40오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034/2 "2020-08-25T15:40:39Z")

</div>

제가 겪고 있는 문제가 이것일까요? [Differences between transactions and locking - makandra dev](https://makandracards.com/makandra/31937-differences-between-transactions-and-locking)

> 두 트랜잭션이 두 개의 스레드에서 동시에 실행될 때, 각 스레드는 다른 트랜잭션의 변경 사항이 성공적으로 커밋되기 전까지는 이를 볼 수 없습니다. 하지만 자신의 변경 사항은 볼 수 있습니다(이것은 단순화된 설명이며, 실제 상황은 훨씬 더 복잡합니다).

---

<div class="post-metadata">

### Author: ![Stefan\_Brighiu\_M](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stefan_brighiu_m/32/174499_2.png) [@Stefan\_Brighiu\_M](https://meta.discourse.org/u/Stefan_Brighiu_M)
#### Post date: [8월 25, 2020, 4:06오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034/3 "2020-08-25T16:06:11Z")

</div>

def self.cast\_custom\_field(key, value, types, return\_array = true)

일반적인 값들이 표시되어 여러 행이 생성된 것 같다는 생각이 듭니다.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [8월 25, 2020, 4:09오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034/4 "2020-08-25T16:09:21Z")

</div>

> [@UserUpdater 클래스의 일관성 없는 동작](https://meta.discourse.org/t/inconsistent-behaviour-in-userupdater-class/161360/7):
>
> Ok, so this one is merged.

이것으로 해결되었어야 합니다. 오래된 버그였습니다.

---

<div class="post-metadata">

### Author: ![Stefan\_Brighiu\_M](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stefan_brighiu_m/32/174499_2.png) [@Stefan\_Brighiu\_M](https://meta.discourse.org/u/Stefan_Brighiu_M)
#### Post date: [8월 25, 2020, 4:14오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034/5 "2020-08-25T16:14:09Z")

</div>

네, 확인했습니다 ☹

불행히도 현재 민감한 데이터를 동시에 업데이트해야 하는 상황에 직면해 있습니다. 엔드포인트를 통해 처리하고 있기 때문에 이런 문제가 발생하는 것 같지만, 이것이 제가 할 수 있는 유일한 방법이며 호출에 대한 통제권이 없습니다. 호출이 1번일 수도 있고 10번일 수도 있습니다.

앱에서 호출을 최소화하고 데이터를 일괄 전송해 볼 수는 있지만, 문제는 호출이 모바일과 외부 서비스라는 두 가지 출처에서 발생한다는 점입니다.

결제 데이터가 관련되어 있어서 걱정됩니다 ☹

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [8월 25, 2020, 4:36오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034/6 "2020-08-25T16:36:15Z")

</div>

이것은 회귀(regression)가 아니라 커스텀 필드의 기존 동작으로 보입니다. 이를 수정하는 몇 가지 방법이 있습니다:

1. 커스텀 필드에 (name, user\_id) 유니크 인덱스를 추가할 수 있습니다.
2. 코드를 DistributedMutex로 감쌀 수 있습니다.
3. 커스텀 필드 대신 테이블 설계를 사용할 수 있습니다.

---

<div class="post-metadata">

### Author: ![Stefan\_Brighiu\_M](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stefan_brighiu_m/32/174499_2.png) [@Stefan\_Brighiu\_M](https://meta.discourse.org/u/Stefan_Brighiu_M)
#### Post date: [8월 25, 2020, 4:44오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034/7 "2020-08-25T16:44:33Z")

</div>

이런 식으로 하면 될까요? 작동하는 것 같습니다.

```
DistributedMutex.synchronize("user_data_update_#{user.id}") do
    user.save_custom_fields(true)
end

```

---

<div class="post-metadata">

### Author: ![Stefan\_Brighiu\_M](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stefan_brighiu_m/32/174499_2.png) [@Stefan\_Brighiu\_M](https://meta.discourse.org/u/Stefan_Brighiu_M)
#### Post date: [8월 25, 2020, 4:51오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034/8 "2020-08-25T16:51:34Z")

</div>

이 외에도, 모든 호출을 한꺼번에 실행하는 대신 하나씩 순서대로 실행하도록 로직을 변경할 예정입니다. 이를 통해 문제를 방지할 수 있기를 바랍니다.

감사합니다. @eviltrout @fzngagan

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [8월 25, 2020, 5:09오후 UTC](https://meta.discourse.org/t/custom-fields-updated-simultaneous-cause-value-to-turn-into-array/162034/9 "2020-08-25T17:09:00Z")

</div>


