aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-09-14 10:11:01 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-09-14 10:17:28 +0900
commitbdc2ba0ec9e1920965841da6e1a392d1ba2816e4 (patch)
treee14fdf318435d2128c749ee7d9bfa31e9c8c625f /activerecord
parentf904700ba4579a36670749d0cfe23cc117d0e389 (diff)
downloadrails-bdc2ba0ec9e1920965841da6e1a392d1ba2816e4.tar.gz
rails-bdc2ba0ec9e1920965841da6e1a392d1ba2816e4.tar.bz2
rails-bdc2ba0ec9e1920965841da6e1a392d1ba2816e4.zip
Don't use `collection.table_name` in `collection_cache_key`
Because `collection.table_name` doesn't respect table alias. Use `collection.arel_attribute` instead.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/collection_cache_key.rb2
-rw-r--r--activerecord/test/cases/collection_cache_key_test.rb18
2 files changed, 19 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/collection_cache_key.rb b/activerecord/lib/active_record/collection_cache_key.rb
index b1937a3c68..f3e6516414 100644
--- a/activerecord/lib/active_record/collection_cache_key.rb
+++ b/activerecord/lib/active_record/collection_cache_key.rb
@@ -13,7 +13,7 @@ module ActiveRecord
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)}"
+ column = connection.column_name_from_arel_node(collection.arel_attribute(timestamp_column))
select_values = "COUNT(*) AS #{connection.quote_column_name("size")}, MAX(%s) AS timestamp"
if collection.has_limit_or_offset?
diff --git a/activerecord/test/cases/collection_cache_key_test.rb b/activerecord/test/cases/collection_cache_key_test.rb
index dbe6857487..c137693211 100644
--- a/activerecord/test/cases/collection_cache_key_test.rb
+++ b/activerecord/test/cases/collection_cache_key_test.rb
@@ -55,6 +55,24 @@ module ActiveRecord
assert_equal last_developer_timestamp.to_s(ActiveRecord::Base.cache_timestamp_format), $3
end
+ test "cache_key for relation with table alias" do
+ table_alias = Developer.arel_table.alias("omg_developers")
+ table_metadata = ActiveRecord::TableMetadata.new(Developer, table_alias)
+ predicate_builder = ActiveRecord::PredicateBuilder.new(table_metadata)
+
+ developers = ActiveRecord::Relation.create(Developer, table_alias, predicate_builder)
+ developers = developers.where(salary: 100000).order(updated_at: :desc)
+ last_developer_timestamp = developers.first.updated_at
+
+ assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\z/, developers.cache_key)
+
+ /\Adevelopers\/query-(\h+)-(\d+)-(\d+)\z/ =~ developers.cache_key
+
+ assert_equal Digest::MD5.hexdigest(developers.to_sql), $1
+ assert_equal developers.count.to_s, $2
+ assert_equal last_developer_timestamp.to_s(ActiveRecord::Base.cache_timestamp_format), $3
+ end
+
test "it triggers at most one query" do
developers = Developer.where(name: "David")