# Preloading custom data and fields in the topic list

**URL:** https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389
**Category:** Development
**Created:** [February 14, 2017, 9:45pm UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389 "2017-02-14T21:45:09Z")
**Posts on this page:** 9
**Page:** 1

<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: [February 14, 2017, 9:45pm UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389/1 "2017-02-14T21:45:10Z")

</div>

Certain plugins need the ability to “preload” data in the topic list.

Some specific examples:

- Discourse solved adds an icon on the topic list when a topic is solved
- Discourse assign will display the username the topic is assigned to on the topic list

To achieve that we have 2 mechanisms:

### Preloading a custom field

If you have any custom fields you need for rendering of the topic list use:

```plaintext
TopicList.preloaded_custom_fields << "my_custom_field"

```

This will force the system to ensure that the Topic object model has the custom field loaded when serializing.

If you do not do that and try to access a custom field an error will be raised.

###Preloading custom data

In some cases you may need to preload custom data that goes beyond a custom field. To do so:

```ruby
TopicList.on_preload do |topics, topic_list|
    # your custom code to load up all topic data in bulk goes here
end

```

This is useful when you need to reach out and preload other models.

By carefully using both these methods you can ensure your plugin does not cause N+1 queries.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 14, 2017, 10:00pm UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389/2 "2017-02-14T22:00:26Z")

</div>

I believe Feature Voting plugin currently has n+1 queries and perhaps for the same reasons?

---

<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: [February 14, 2017, 10:01pm UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389/3 "2017-02-14T22:01:30Z")

</div>

very possible, I only added the ability to preload custom data today.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [February 14, 2017, 10:03pm UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389/4 "2017-02-14T22:03:58Z")

</div>

This will make a lot of plugins use way less code, like my signatures plugin.

---

<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: [February 14, 2017, 10:04pm UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389/5 "2017-02-14T22:04:35Z")

</div>

Does signature show up in topic list? scary …

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [February 14, 2017, 10:06pm UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389/6 "2017-02-14T22:06:34Z")

</div>

> [@sam](#):
>
> Does signature show up in topic list? scary …

Oh no, misread as post list. 😄

Will there be an analogous feature to the post stream?

---

<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: [February 14, 2017, 10:07pm UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389/7 "2017-02-14T22:07:03Z")

</div>

I would be happy to add an analogous feature, makes sense there as well.

---

<div class="post-metadata">

### Author: ![joebuhlig](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/joebuhlig/32/193054_2.png) [@joebuhlig](https://meta.discourse.org/u/joebuhlig)
#### Post date: [February 15, 2017, 2:04am UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389/8 "2017-02-15T02:04:19Z")

</div>

> [@codinghorror](#):
>
> I believe Feature Voting plugin currently has n+1 queries

It used to. I think I’ve cleaned most of those up. I don’t know of any others.

[https://github.com/discourse/discourse-feature-voting/commit/135eaf8a5ec0e37b64adf10971384ee88abec580](https://github.com/discourse/discourse-feature-voting/commit/135eaf8a5ec0e37b64adf10971384ee88abec580)

> [@codinghorror](#):
>
> and perhaps for the same reasons?

I had to preload the custom fields due to errors if the data was blank. It was tricky to debug at first because I had existing data so I couldn’t replicate the errors reported. Then someone mentioned this process in Babble. Once I implemented it, the errors were gone. I never looked into it further than that.

---

<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: [January 17, 2025, 3:36pm UTC](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389/9 "2025-01-17T15:36:46Z")

</div>

Just for anyone reading this in 2025 I’ve written an updated version on the same topic

> [@Preloading data and dealing with N+1 Query problems](https://meta.discourse.org/t/preloading-data-and-dealing-with-n-1-query-problems/347688):
>
> When working with the Discourse rails application, whether building a plugin, or making a pull request to discourse/discourse there are a number of contexts where you’ll encounter N+1 query problems. This topic explains how to handle data preloading to address such problems and keep Discourse performant. N+1 Query Problems in Rails First, if you’re not already familiar with N+1 query problems, and how Rails addresses them, check out this section of the guides. Topics The N+1 issues to look ou…
