sam
(Sam Saffron)
February 14, 2017, 9:45pm
1
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:
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:
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.
9 Likes
I believe Feature Voting plugin currently has n+1 queries and perhaps for the same reasons?
sam
(Sam Saffron)
February 14, 2017, 10:01pm
3
very possible, I only added the ability to preload custom data today.
Falco
(Falco)
February 14, 2017, 10:03pm
4
This will make a lot of plugins use way less code, like my signatures plugin.
1 Like
sam
(Sam Saffron)
February 14, 2017, 10:04pm
5
Does signature show up in topic list? scary …
Falco
(Falco)
February 14, 2017, 10:06pm
6
Oh no, misread as post list.
Will there be an analogous feature to the post stream?
4 Likes
sam
(Sam Saffron)
February 14, 2017, 10:07pm
7
I would be happy to add an analogous feature, makes sense there as well.
4 Likes
joebuhlig
(Joe Buhlig)
February 15, 2017, 2:04am
8
It used to. I think I’ve cleaned most of those up. I don’t know of any others.
committed 02:44PM - 20 Oct 16 UTC
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.
3 Likes