diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-09-14 10:11:01 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-09-14 10:17:28 +0900 |
commit | bdc2ba0ec9e1920965841da6e1a392d1ba2816e4 (patch) | |
tree | e14fdf318435d2128c749ee7d9bfa31e9c8c625f /activerecord/test/cases | |
parent | f904700ba4579a36670749d0cfe23cc117d0e389 (diff) | |
download | rails-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/test/cases')
-rw-r--r-- | activerecord/test/cases/collection_cache_key_test.rb | 18 |
1 files changed, 18 insertions, 0 deletions
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") |