# Inconsistent behaviour in UserUpdater class

**URL:** https://meta.discourse.org/t/inconsistent-behaviour-in-userupdater-class/161360
**Category:** Development
**Created:** [19 augustus 2020 om 09:58 UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-userupdater-class/161360 "2020-08-19T09:58:37Z")
**Posts on this page:** 5
**Page:** 1

<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: [19 augustus 2020 om 09:58 UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-userupdater-class/161360/1 "2020-08-19T09:58:37Z")

</div>

@eviltrout

Repro steps:

1. Let’s create a user field and it has id `1`.
2. Run the second line of the below code twice.

## Case 1

```plaintext
up = UserUpdater.new(Discourse.system_user, User.find(1))
up.update({:custom_fields=>{:user_field_1=>"abc"}})

```

1. You’ll see `abc` twice when you go to `/my/preferences/profile`

Surprisingly, when the profile is updated via `/my/preferences/profile`, it does so perfectly fine.

## Case 2

This seems to work fine

```plaintext
up = UserUpdater.new(Discourse.system_user, User.find(1))
params = ActionController::Parameters.new({:custom_fields=>{:user_field_1=>"abc"}})
up.update(params.permit!)

```

What happens is, in Case 1, the value is pushed to an array and is displayed as comma-separated values on `/my/preferences/profile`.

Case 2 seems to do the correct thing i.e. it replaces the current value with the new one but wrapping it in `ActionController::Parameters` is unnatural.

> <https://github.com/discourse/discourse/blob/3e49c5b4d875654f3357583828086fe8fee104be/app/controllers/users_controller.rb#L160>

---

<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: [19 augustus 2020 om 13:59 UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-userupdater-class/161360/3 "2020-08-19T13:59:55Z")

</div>

This does seem like a valid bug and should be fixed, thanks for reporting it!

---

<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: [20 augustus 2020 om 06:28 UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-userupdater-class/161360/5 "2020-08-20T06:28:34Z")

</div>

My analysis comes down to this.

```plaintext
# case 1
u1 = User.find(1)
u1.custom_fields[:user_field_1] = "abc"
u1.save

# case 2
u1.custom_fields["user_field_1"] = "abc"
u1.save

```

In case 1, A new `UserCustomField` is created per call to `u1.save`

 ![Screenshot 2020-08-20 at 11.52.09 AM](https://global.discourse-cdn.com/meta/original/3X/3/a/3a26e5d24f52fd2d97cc7350253040ad51455bde.png)

In case 2, All `UserCustomField` with the name ‘user\_field\_1’ are deleted from the db  
except the specified.

![Screenshot 2020-08-20 at 11.56.49 AM](https://global.discourse-cdn.com/meta/original/3X/e/3/e3082d7f74f04f21a31a9d78205dc3497ee9b89f.png)

All and all the `symbol` and `string` versions of the same key are treated differently in the `HasCustomFields` mixin.

---

<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: [20 augustus 2020 om 09:35 UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-userupdater-class/161360/6 "2020-08-20T09:35:55Z")

</div>

Ok, I went for it. Here’s the PR which fixes the issue

[https://github.com/discourse/discourse/pull/10486](https://github.com/discourse/discourse/pull/10486)

---

<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: [25 augustus 2020 om 15:28 UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-userupdater-class/161360/7 "2020-08-25T15:28:58Z")

</div>

Ok, so this one is merged.
