# Adding to user serializer does not work for the current user object

**URL:** <https://meta.discourse.org/t/adding-to-user-serializer-does-not-work-for-the-current-user-object/319809>\
**Category:** Development\
**Created:** [August 2, 2024, 9:23am 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

## AI-generated summary

[Harshit_Dave](/t/-/319809/1) reported an issue where a custom field added to the user model was not appearing in the current user object when using the `UserSerializer`. They attempted to include the field using both `attributes` and `add_to_serializer` within the `UserSerializer` class, but neither approach worked. [Lhc_fl](/t/-/319809/2) suggested that since the request involves the current user, the modification should be made to the `CurrentUserSerializer` instead, providing an example of how to add fields to it. [Harshit_Dave](/t/-/319809/3) tried this approach but encountered a `NoMethodError`. [Lhc_fl](/t/-/319809/4) clarified that in the `CurrentUserSerializer` context, `object` refers to the `User` instance, not the serializer itself, and advised accessing the custom field via `object.custom_fields["custom_thumbnail_style_dropdown"]`. This solution resolved the issue for [Harshit_Dave](/t/-/319809/5).

<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:** [August 2, 2024, 9:23am 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>

I have added custom field in user model but when I tried to fetch in current user object I did not get the custom field. how can I fetch the custom field?

I tried below code

> ```
> 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
> 
> ```

even I tried `object.custom_thumbnail_style_dropdown` in the defination but it didnt’ work.

Also tried below code

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

Any help would be appreciated!

---

<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:** [August 2, 2024, 9:55am 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>

Hi @Harshit_Dave

> [@Harshit\_Dave](#):
>
> when I tried to fetch in current user object

Then you should add to CurrentUserSerializer instead of UserSerializer 😉

For example:

```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:** [August 2, 2024, 10:18am 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>

Hi @Lhc_fl

Thanks for messaging!

I tried using :current\_user serializer but then I got `NoMethodError` error

I tried below code. I am not sure about code as I am new to 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:** [August 2, 2024, 10:22am 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>

Here `object` is a instance of `User`, not a `UserSerializer`. Therefore, you should write something like this:

```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:** [August 2, 2024, 10:26am 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>

It worked for me. Thank you so much!

---

<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:** [September 1, 2024, 10:27am 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>

Closed on September 1, 2024, 10:27am UTC

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