module ActiveRecord

Actually this all depends on retrieve_cache_key(key) from active_support: def retrieve_cache_key(key)

case
  when key.respond_to?(:cache_key) then key.cache_key
  when key.is_a?(Array)            then key.map { |element| retrieve_cache_key(element) }.to_param
  when key.respond_to?(:to_a)      then retrieve_cache_key(key.to_a)
  else                                  key.to_param
end.to_s

end in rails 4 there is no cache_key in relations so if we deliver relation as key in rails 4 it will convert to array first in rails 5 there is cache_key, the method below is copy+paste from rails 5 except it doesn't use rails 5 collection_cache_key, and use it's own algorithm for cache_key