# Добавление к сериализатору пользователя не работает для текущего объекта пользователя

**URL:** https://meta.discourse.org/t/adding-to-user-serializer-does-not-work-for-the-current-user-object/319809
**Category:** Development
**Created:** [02.Август.2024 09:23:50 UTC](https://meta.discourse.org/t/adding-to-user-serializer-does-not-work-for-the-current-user-object/319809 "2024-08-02T09:23:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Harshit\_Dave](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/harshit_dave/32/379059_2.png) [@Harshit\_Dave](https://meta.discourse.org/u/Harshit_Dave)
#### Post date: [02.Август.2024 09:23:50 UTC](https://meta.discourse.org/t/adding-to-user-serializer-does-not-work-for-the-current-user-object/319809/1 "2024-08-02T09:23:50Z")

</div>

Я добавил пользовательское поле в модель пользователя, но при попытке получить его в объекте текущего пользователя поле не отображается. Как можно получить пользовательское поле?

Я пробовал следующий код:

```plaintext
require_dependency 'user_serializer'
class ::UserSerializer
  attributes :custom_thumbnail_style_dropdown
  def custom_thumbnail_style_dropdown
    object.custom_fields[custom_thumbnail_style_dropdown]
  end
end

```

Также пробовал использовать `object.custom_thumbnail_style_dropdown` в определении, но это не сработало.

Пробовал и такой вариант:

```plaintext
add_to_serializer(:user, :custom_thumbnail_style_dropdown, false) do
  object.custom_thumbnail_style_dropdown
end

```

Буду благодарен за любую помощь!

---

<div class="post-metadata">

### Author: ![Lhc\_fl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lhc_fl/32/268115_2.png) [@Lhc\_fl](https://meta.discourse.org/u/Lhc_fl)
#### Post date: [02.Август.2024 09:55:37 UTC](https://meta.discourse.org/t/adding-to-user-serializer-does-not-work-for-the-current-user-object/319809/2 "2024-08-02T09:55:37Z")

</div>

Привет @Harshit_Dave

> [@Harshit Dave](#):
>
> когда я пытался получить объект текущего пользователя

Тогда вам нужно добавить это в CurrentUserSerializer, а не в UserSerializer 😉

Например:

```ruby
  add_to_serializer(:current_user, :can_create_poll) do
    scope.user&.staff? || scope.user&.in_any_groups?(SiteSetting.poll_create_allowed_groups_map)
  end

```

---

<div class="post-metadata">

### Author: ![Harshit\_Dave](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/harshit_dave/32/379059_2.png) [@Harshit\_Dave](https://meta.discourse.org/u/Harshit_Dave)
#### Post date: [02.Август.2024 10:18:29 UTC](https://meta.discourse.org/t/adding-to-user-serializer-does-not-work-for-the-current-user-object/319809/3 "2024-08-02T10:18:29Z")

</div>

Привет, @Lhc_fl

Спасибо за обращение!

Я попытался использовать сериализатор :current\_user, но получил ошибку `NoMethodError`.

Я попробовал следующий код. Не уверен в нём, так как я новичок в Discourse:

> add\_to\_serializer(:current\_user, :custom\_thumbnail\_style\_dropdown) do  
> object.custom\_thumbnail\_style\_dropdown  
> end

---

<div class="post-metadata">

### Author: ![Lhc\_fl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lhc_fl/32/268115_2.png) [@Lhc\_fl](https://meta.discourse.org/u/Lhc_fl)
#### Post date: [02.Август.2024 10:22:28 UTC](https://meta.discourse.org/t/adding-to-user-serializer-does-not-work-for-the-current-user-object/319809/4 "2024-08-02T10:22:28Z")

</div>

Здесь `object` — это экземпляр `User`, а не `UserSerializer`. Поэтому вам следует написать что-то вроде этого:

```ruby
add_to_serializer(:current_user, :custom_thumbnail_style_dropdown) do
    object.custom_fields["custom_thumbnail_style_dropdown"]
end

```

---

<div class="post-metadata">

### Author: ![Harshit\_Dave](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/harshit_dave/32/379059_2.png) [@Harshit\_Dave](https://meta.discourse.org/u/Harshit_Dave)
#### Post date: [02.Август.2024 10:26:51 UTC](https://meta.discourse.org/t/adding-to-user-serializer-does-not-work-for-the-current-user-object/319809/5 "2024-08-02T10:26:51Z")

</div>

У меня всё получилось. Большое спасибо!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [01.Сентябрь.2024 10:27:08 UTC](https://meta.discourse.org/t/adding-to-user-serializer-does-not-work-for-the-current-user-object/319809/6 "2024-09-01T10:27:08Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
