# 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:** [14 فبراير 2017، 9:45م 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:** 1\
**Showing post:** 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:** [14 فبراير 2017، 9:45م 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.

---

_[View the full topic](https://meta.discourse.org/t/preloading-custom-data-and-fields-in-the-topic-list/57389)._
