You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have helper which I use for caching and accessing some data for each item in a a collection in my views.
I have a helper which looks like this:
classMyHelperdefhuman_name(id)id_to_human_name_map[id]endprivate# Should be loaded once per request and cached in memory for the duration of the current# request; cached in memcached for the `expires` duration.# returns something like: {23843 => "Name 1", 2387454387 => "Name 2"}defid_to_human_name_map# Memorizing to avoid the roundtrip to memcached more than once per request@id_to_human_name_map ||= beginRails.cache.fetch("id_to_human_name_map",expires_in: 10.minutes)do#Some slow stuff I really don't wanna do for every item in my collectionendendendend
The problem is however, that when I have a controller action like this:
defindex@fruits=FruitDecorator.decorate_collection(Fruit.all)@fruits.eachdo |f|
# This triggers rails to get a roundtrip to memcached for each item, # even though I try to cache the result within the request with @id_to_human_name_map ||=f.human_nameendend
Then @id_to_human_name_map is nil for every item. So the instance variable seems to be set in the scope of a new instance of the helper every time, instead of in the scope of the entire request (controller).
Is this an intended behaviour of draper? Is there any way to avoid it?
I'm running draper 2.1.0
The text was updated successfully, but these errors were encountered:
NielsKSchjoedt
changed the title
Calling a helper from within a decorator renders a new instance every time
Memorization with instance variables in helpers does not work
Feb 26, 2018
I have helper which I use for caching and accessing some data for each item in a a collection in my views.
I have a helper which looks like this:
And a decorator like this:
The problem is however, that when I have a controller action like this:
Then
@id_to_human_name_map
is nil for every item. So the instance variable seems to be set in the scope of a new instance of the helper every time, instead of in the scope of the entire request (controller).Is this an intended behaviour of draper? Is there any way to avoid it?
I'm running draper 2.1.0
The text was updated successfully, but these errors were encountered: