diff options
author | Prathamesh Sonpatki <csonpatki@gmail.com> | 2016-01-16 12:47:26 +0530 |
---|---|---|
committer | Prathamesh Sonpatki <csonpatki@gmail.com> | 2016-01-16 12:47:26 +0530 |
commit | 8126925447b6ba2e26c4bca4b4e10fb99803483b (patch) | |
tree | 0beb18b3cb4b05ade837ba4402fa900aa269cf5b /activerecord/lib/active_record/collection_cache_key.rb | |
parent | d3e2f1069857d278d162af2d3de1d9a3c3197f4b (diff) | |
download | rails-8126925447b6ba2e26c4bca4b4e10fb99803483b.tar.gz rails-8126925447b6ba2e26c4bca4b4e10fb99803483b.tar.bz2 rails-8126925447b6ba2e26c4bca4b4e10fb99803483b.zip |
Fix ActiveRecord::Relation#cache_key for loaded empty collection
- Before this patch if we try to find cache_key of a loaded but empty
collection it used to give error because of trying to call `updated_at`
on `nil` value generated by
`collection.max_by(×tamp_column).public_send(timestamp_column)`.
- This commit fixes above error by checking if size is greater than zero
or not.
Diffstat (limited to 'activerecord/lib/active_record/collection_cache_key.rb')
-rw-r--r-- | activerecord/lib/active_record/collection_cache_key.rb | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/collection_cache_key.rb b/activerecord/lib/active_record/collection_cache_key.rb index 3c4ca3d116..b0e555038e 100644 --- a/activerecord/lib/active_record/collection_cache_key.rb +++ b/activerecord/lib/active_record/collection_cache_key.rb @@ -7,7 +7,9 @@ module ActiveRecord if collection.loaded? size = collection.size - timestamp = collection.max_by(×tamp_column).public_send(timestamp_column) + if size > 0 + timestamp = collection.max_by(×tamp_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)}" |