# Custom field casting affected by recent update?

**URL:** https://meta.discourse.org/t/custom-field-casting-affected-by-recent-update/122746
**Category:** Development
**Created:** [July 11, 2019, 11:34pm UTC](https://meta.discourse.org/t/custom-field-casting-affected-by-recent-update/122746 "2019-07-11T23:34:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [July 11, 2019, 11:34pm UTC](https://meta.discourse.org/t/custom-field-casting-affected-by-recent-update/122746/1 "2019-07-11T23:34:42Z")

</div>

There have been multiple bug reports for the [events plugin](https://meta.discourse.org/t/events-plugin-calendar) in the last two days that all seem to be type-casting issues with custom fields. The events plugin hasn’t been substantively updated in a few months.

> [@Events Plugin calendar](https://meta.discourse.org/t/events-plugin-calendar/69776/482):
>
> Is the bellow error related to this plugin? (also posted [here](https://meta.discourse.org/t/topic-in-a-category-make-unreachable-the-category-and-user-activity-and-the-topic-itself/122644)) a user created a topic by copy pasting some content from a facebook page; this has crashed the topic iteslf, the category and his activities page. (the rest of the topics and categories of the site remained accessible) I managed to destroy the topic from the console and everything went back to normal for the user and category. Is this error related to the mentioned plugin [“discourse-events”](https://meta.discourse.org/t/events-plugin-calendar/69776)? here follows the errors: info: NoMet…

The plugin.rb file casts `event_start` as an integer:

```plaintext
Topic.register_custom_field_type('event_start', :integer)

```

The error is being thrown here:

```plaintext
def has_event?
  self.custom_fields['event_start']&.nonzero?
end

```

The error itself follows this format:

```plaintext
NoMethodError (undefined method `nonzero?' for [1563127206, 1563127206]:Array)

```

As far as I understand `register_custom_field_type` it should ensure that the custom\_field always returns as the defined type (perhaps I’m misunderstanding it).

Looking at the `has_custom_fields.rb` concern, there have been a few changes in the past week that could have affected this, in particular

[https://github.com/discourse/discourse/commit/72bac61c90044407f7a2029b15f6ac043a8bccf0#diff-712e064741bd664591e6392a52eb409e](https://github.com/discourse/discourse/commit/72bac61c90044407f7a2029b15f6ac043a8bccf0#diff-712e064741bd664591e6392a52eb409e)

@eviltrout Any thoughts on this?

---

<div class="post-metadata">

### Author: ![icaria36](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/icaria36/32/426431_2.png) [@icaria36](https://meta.discourse.org/u/icaria36)
#### Post date: [July 12, 2019, 9:56am UTC](https://meta.discourse.org/t/custom-field-casting-affected-by-recent-update/122746/2 "2019-07-12T09:56:59Z")

</div>

In case this is indeed the bug causing problems, here I explain the consequences (it may make your site unusable):

> [@Events Plugin calendar](https://meta.discourse.org/t/events-plugin-calendar/69776/484):
>
> This patch seems to work! I can tell that the difference between having this patch or not for us meant having a broken site or a(n apparently) fully functional one. These were the effects of the bug: Discourse not rendering to anonymous users beyond the header (totally unusable site). For registered users, probably non-cached content (i.e. Latest lists) wouldn’t render, and the rest was a hit or miss. Strangely enough, from three admins two would lose access to the web UI completely while a t…

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [July 12, 2019, 10:24am UTC](https://meta.discourse.org/t/custom-field-casting-affected-by-recent-update/122746/3 "2019-07-12T10:24:27Z")

</div>

> [@angus](#):
>
> The error itself follows this format:
> 
> ```plaintext
> NoMethodError (undefined method `nonzero?' for [1563127206, 1563127206]:Array)
> 
> ```

This _was_ a known bug with custom fields.

In very specific conditions, it would save the value multiple times, thus creating an Array, when you only want an Integer.

You can fix this by making sure there’s only 1 row per custom field in the database.

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [July 12, 2019, 10:46am UTC](https://meta.discourse.org/t/custom-field-casting-affected-by-recent-update/122746/4 "2019-07-12T10:46:06Z")

</div>

> [@zogstrip](#):
>
> This _was_ a known bug with custom fields.

Yes, it’s cropped up a few times in the past. This latest rash seems to coincide with recent work on the `has_custom_fields.rb` concern, so there may be something to review there.

This doesn’t fix it, but helps to address it if it arises:

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

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [January 10, 2020, 4:07am UTC](https://meta.discourse.org/t/custom-field-casting-affected-by-recent-update/122746/5 "2020-01-10T04:07:54Z")

</div>

> [@angus](#):
>
> NoMethodError (undefined method `nonzero?’ for [1563127206, 1563127206]:Array)

My general recommendation here is that as a plugin author you should always add indexes in a migration to properly enforce the constraint. In fact majority of plugins we right these days avoid custom fields unless absolutely needed and prefer using custom tables which are far easier to reason about. In this specific case you want an index of:

`create unique index idxStartEvent on topic_custom_fields(topic_id) where name = 'start_event'`

Not sure what else really we need to do in core here, we have considered a revamp of custom fields but are somewhat worried about it. One thing I am considering is simply dropping array support from custom fields cause they are just causing enormous amounts of issues over the years.

---

<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: [August 25, 2020, 6:22am UTC](https://meta.discourse.org/t/custom-field-casting-affected-by-recent-update/122746/6 "2020-08-25T06:22:47Z")

</div>

> [@zogstrip](#):
>
> In very specific conditions, it would save the value multiple times, thus creating an Array, when you only want an Integer.

Sorry for bumping this up but one of the conditions is when you use a symbol index i.e. `custom_fields[:hello]` while updating the existing value, it added another field instead of updating and hence the giving an array. This might be the only condition IMO.

This should fix the caused side-effect most certainly.  
[https://github.com/discourse/discourse/pull/10486](https://github.com/discourse/discourse/pull/10486)
