# How to add custom field to topic?

**URL:** https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948
**Category:** Development
**Created:** [2017年二月25日 16:46 UTC](https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948 "2017-02-25T16:46:16Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Janno\_Liivak](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/janno_liivak/32/352215_2.png) [@Janno\_Liivak](https://meta.discourse.org/u/Janno_Liivak)
#### Post date: [2017年二月25日 16:46 UTC](https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948/1 "2017-02-25T16:46:16Z")

</div>

I tried to add custom field to Topic but doing it the same way as for category gives me error:

`Property set failed: object in path "custom_fields" could not be found or was destroyed.`

Example of how it works successfully for category:

> <https://github.com/jannolii/discourse-bump-topic/blob/master/assets/javascripts/discourse/pre-initializers/extend-category-for-bump-topics.js.es6#L9-L23>

Suggestions, anyone?

---

<div class="post-metadata">

### Author: ![Janno\_Liivak](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/janno_liivak/32/352215_2.png) [@Janno\_Liivak](https://meta.discourse.org/u/Janno_Liivak)
#### Post date: [2017年三月5日 22:01 UTC](https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948/2 "2017-03-05T22:01:17Z")

</div>

I’m really stuck here ☹  
I just don’t get it … what do I have to add to plugin.rb in order my plugin to have access to custom fields? As I understood, topics should already have custom field functionality by default.

I have code in pre-initializer…

```
    Topic.reopen({

      @property('custom_fields.bumped_at_with_button')
      bumped_at_with_button: {
        get(date_value) {
          return new Date(date_value);
        },
        set(value) {
          this.set('custom_fields.bumped_at_by_button', (value ? value.toISOString() : ''));
          return value;
        }
      }

    });

```

I have following section in plugin.rb…

```
add_to_serializer(:topic_view, :bumped_at_with_button, false) {
  object.topic.custom_fields['bumped_at_with_button']
}

```

But when setting value with…

```
topic.set('custom_fields.bumped_at_with_button')

```

… I get the message I pointed out in topic ☹

Help… anybody?

---

<div class="post-metadata">

### Author: ![jafeth.diazc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jafeth.diazc/32/101197_2.png) [@jafeth.diazc](https://meta.discourse.org/u/jafeth.diazc)
#### Post date: [2017年三月6日 01:30 UTC](https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948/3 "2017-03-06T01:30:21Z")

</div>

Hello @Janno_Liivak,

As far as I know, `TopicViewSerializer` does not include a `custom_fields` field by default. With your call to `add_to_serializer` you could access directly to a field called `bumped_at_with_button`. However, If you want to access to that field through a `custom_fields` field, then I think you should change your code to something like:

```
add_to_serializer(:topic_view, :custom_fields, false) {
  object.topic.custom_fields.slice(:bumped_at_with_button)
}

```

---

<div class="post-metadata">

### Author: ![Janno\_Liivak](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/janno_liivak/32/352215_2.png) [@Janno\_Liivak](https://meta.discourse.org/u/Janno_Liivak)
#### Post date: [2017年三月7日 19:43 UTC](https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948/4 "2017-03-07T19:43:41Z")

</div>

Thanks. It worked. But still … the value in setter function is always null. Could you point me in a right direction with

```
Topic.update(topic, props);

```

How should custom\_fields.bumped\_at\_with\_button be passed to props in order to be saved?

I’ve tried something, but it doesn’t work ☹ In following code only bumped\_at get’s saved to database… and sadly no new record in topic\_custom\_fields table.

```
import Topic from 'discourse/models/topic';

export default {
  actions: {
    clickButton(topic) {
      const current_date = new Date();
      topic.set('bumped_at', current_date);
      topic.set('custom_fields.bumped_at_with_button', current_date);
      const props = topic.getProperties('bumped_at', 'custom_fields');
      Topic.update(topic, props);
    }
  }
};

```

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [2018年四月20日 22:19 UTC](https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948/5 "2018-04-20T22:19:57Z")

</div>

Hey @Janno_Liivak

Did you find how to do it? I’m looking to the same at the moment

Thanks!

---

<div class="post-metadata">

### Author: ![Janno\_Liivak](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/janno_liivak/32/352215_2.png) [@Janno\_Liivak](https://meta.discourse.org/u/Janno_Liivak)
#### Post date: [2018年四月21日 13:12 UTC](https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948/6 "2018-04-21T13:12:22Z")

</div>

Yep. But as it was a long time ago don’t remember exactly what I did. I managed to build two plugins eventually. Maybe you can find your answers there?

> [@Trading Buttons](https://meta.discourse.org/t/trading-buttons-buy-sell-exchange/71308):
>
> information_sourceSummary This plugin allows the category to show trading buttons (buy, sell, exchange) above the topic title.hammer_and_wrenchRepository Link [https://github.com/jannolii/discourse-topic-trade-buttons](https://github.com/jannolii/discourse-topic-trade-buttons)open_bookInstall Guide [How to install plugins in Discourse](https://meta.discourse.org/t/install-plugins-in-discourse/19157) I have created a simple plugin that allows the category to show trading buttons above the topic title. It still needs unit tests, but it’s usable as a simple solution for sell/purchase/exchange ty…

Second one isn’t here but you can find it on github.

[https://github.com/jannolii/discourse-bump-topic](https://github.com/jannolii/discourse-bump-topic)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [2018年四月24日 23:41 UTC](https://meta.discourse.org/t/how-to-add-custom-field-to-topic/57948/7 "2018-04-24T23:41:12Z")

</div>

Please note that sensitive info may be stored in topic custom fields - so don’t just dump them all out. Plugins should be selecting out just the fields they’re using.
