# How to set category custom fields?

**URL:** https://meta.discourse.org/t/how-to-set-category-custom-fields/135560
**Category:** Support
**Created:** [December 10, 2019, 1:45pm UTC](https://meta.discourse.org/t/how-to-set-category-custom-fields/135560 "2019-12-10T13:45:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_P](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex_p/32/163548_2.png) [@Alex\_P](https://meta.discourse.org/u/Alex_P)
#### Post date: [December 10, 2019, 1:45pm UTC](https://meta.discourse.org/t/how-to-set-category-custom-fields/135560/1 "2019-12-10T13:45:16Z")

</div>

I want to set `import_id` of one category manually, to be able to import remaining topics from an old forum.

I tried this in `rails c` but it does not seem to work:

```plaintext
[11] pry(main)> Category.find(3).custom_fields
=> {}
[12] pry(main)> Category.find(3).custom_fields['import_id'] = '50'
=> "50"
[13] pry(main)> Category.find(3).custom_fields
=> {}

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [December 10, 2019, 3:38pm UTC](https://meta.discourse.org/t/how-to-set-category-custom-fields/135560/2 "2019-12-10T15:38:06Z")

</div>

I think you need to do something like

```
 CategoryCustomField.create(category_id: 3,...) 

```

Look at the code in the importers for an example.

---

<div class="post-metadata">

### Author: ![Alex\_P](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex_p/32/163548_2.png) [@Alex\_P](https://meta.discourse.org/u/Alex_P)
#### Post date: [December 10, 2019, 3:51pm UTC](https://meta.discourse.org/t/how-to-set-category-custom-fields/135560/3 "2019-12-10T15:51:51Z")

</div>

> [@pfaffman](#):
>
> Look at the code in the importers for an example.

Yeah, finally found it.

```ruby
    new_category.custom_fields["import_id"] = import_id if import_id
    new_category.save!

```

So looks like I just need to call `save` 🙂

I got confused because for `SiteSetting` it is not needed.

```plaintext
[14] pry(main)> c = Category.find(3)
=> #<Category:0x000056548fd948a8
 id: 3,
...
[15] pry(main)> c.custom_fields['import_id'] = '50'
=> "50"
[16] pry(main)> c.custom_fields
=> {"import_id"=>"50"}
[17] pry(main)> c.save!
=> true
[18] pry(main)> Category.find(3).custom_fields
=> {"import_id"=>"50"}

```

---

<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: [January 9, 2020, 4:03pm UTC](https://meta.discourse.org/t/how-to-set-category-custom-fields/135560/4 "2020-01-09T16:03:56Z")

</div>

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