Collection caching: render to strings

47 views
Skip to first unread message

Gleb Mazovetskiy

unread,
Mar 1, 2017, 4:55:37 AM3/1/17
to Ruby on Rails: Core
Hi, I am a contributor to Thredded, a Rails forums engine, and we have an interesting problem.

We currently all cache the posts on a topic page using collection caching.

This requires the entire post to be cached due to the current collection caching interface,
even though only the post content itself is slow to render. That's was not a large problem for us until now.

However, @EllaNancyFay is currently working on a feature for Thredded that requires something that cannot be cached
to be inserted at the end of the post partial, but that's not possible via the current render collection: ..., cache: ... interface.

I'm wondering if a feature that lets us render a collection to Strings would belong in Rails core?

So a method such as render_to_strings(partial:, collection:, cache:),
returning an Array of [ [item, "rendered html"], ... ].

Gleb Mazovetskiy

unread,
Mar 5, 2017, 4:06:14 AM3/5/17
to Ruby on Rails: Core

Kasper Timm Hansen

unread,
Mar 20, 2017, 3:15:37 PM3/20/17
to rubyonra...@googlegroups.com
Been a while since I’ve last worked with that code but doesn’t a collection cache render return a collection of rendered HTML strings?

I think you might be able to stitch the objects and their rendered HTML together like this:

    @posts.zip(render(partial: ’posts/post’, collection: @posts, cached: true))

Though overall, I don’t think we’re going to change the interface here. Thanks! :)

Den 5. mar. 2017 kl. 10.06 skrev Gleb Mazovetskiy <glex...@gmail.com>:

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
Kasper

Gleb Mazovetskiy

unread,
Mar 20, 2017, 4:44:46 PM3/20/17
to rubyonra...@googlegroups.com

No, a collection cache render returns a single string.


You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-core/Cx0GXz_k7Dc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-co...@googlegroups.com.

Kasper Timm Hansen

unread,
Mar 21, 2017, 2:45:24 PM3/21/17
to rubyonra...@googlegroups.com

Gleb Mazovetskiy

unread,
Mar 21, 2017, 2:59:44 PM3/21/17
to rubyonra...@googlegroups.com
If you mean whether cache_collection_render is usable, I think it would be but it is currently private!

The implementation I've linked to above does use PartialRenderer, and effectively reimplements cache_collection_render using only public APIs.
Reply all
Reply to author
Forward
0 new messages