@@ -85,7 +85,7 @@ def write_collection(name, value, **opts)
8585 raise TypeError , "Can't cache more than one type of object via write_collection"
8686 end
8787
88- data = [ types [ 0 ] . to_s , * value . map ( & :id ) ]
88+ data = normalize_collection ( value )
8989 namespaced = construct_ns_key ( name , include_community : include_community ( opts ) )
9090 @underlying . write ( namespaced , data , **opts )
9191 end
@@ -100,6 +100,11 @@ def read_collection(name, **opts)
100100 namespaced = construct_ns_key ( name , include_community : include_community ( opts ) )
101101 data = @underlying . read ( namespaced , **opts )
102102 return nil if data . nil?
103+
104+ if data . is_a? ( ActiveRecord ::Relation )
105+ data = normalize_collection ( data )
106+ end
107+
103108 type = data . slice! ( 0 )
104109 begin
105110 type . constantize . where ( id : data )
@@ -145,5 +150,12 @@ def construct_ns_key(key, include_community: true)
145150 c_id = RequestContext . community_id if include_community
146151 "#{ Rails . env } ://#{ [ c_id , key ] . compact . join ( '/' ) } "
147152 end
153+
154+ # Normalizes a given ActiveRecord collection for use with the cache
155+ # @param value [ActiveRecord::Relation] collection to normalize
156+ # @return [Array(String, *Integer)]
157+ def normalize_collection ( value )
158+ [ value [ 0 ] . class . to_s , *value . map ( &:id ) ]
159+ end
148160 end
149161end
0 commit comments