提交 a688c031 编写于 作者: M Matthew Draper

Merge pull request #23080 from prathamesh-sonpatki/fix-cache-key-for-loaded-empty-collection

Fix ActiveRecord::Relation#cache_key for loaded empty collection
......@@ -7,7 +7,9 @@ def collection_cache_key(collection = all, timestamp_column = :updated_at) # :no
if collection.loaded?
size = collection.size
timestamp = collection.max_by(&timestamp_column).public_send(timestamp_column)
if size > 0
timestamp = collection.max_by(&timestamp_column).public_send(timestamp_column)
end
else
column_type = type_for_attribute(timestamp_column.to_s)
column = "#{connection.quote_table_name(collection.table_name)}.#{connection.quote_column_name(timestamp_column)}"
......
......@@ -66,5 +66,13 @@ class CollectionCacheKeyTest < ActiveRecord::TestCase
developers = projects(:active_record).developers
assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\Z/, developers.cache_key)
end
test "cache_key for loaded collection with zero size" do
Comment.delete_all
posts = Post.includes(:comments)
empty_loaded_collection = posts.first.comments
assert_match(/\Acomments\/query-(\h+)-0\Z/, empty_loaded_collection.cache_key)
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册