diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-08-13 17:51:07 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-08-13 17:51:07 -0300 |
commit | 0168ce10a31ff2a00e2bce1c81ffbb25f0500281 (patch) | |
tree | c04467e96924fe3c69db9301c60a3f36c0d36ef8 /activerecord/lib/active_record/associations | |
parent | 33dc653466c0058740611259956712a9027801b3 (diff) | |
parent | 543523045112c5f5920c486e6fcf2d7e1ffedf5a (diff) | |
download | rails-0168ce10a31ff2a00e2bce1c81ffbb25f0500281.tar.gz rails-0168ce10a31ff2a00e2bce1c81ffbb25f0500281.tar.bz2 rails-0168ce10a31ff2a00e2bce1c81ffbb25f0500281.zip |
Merge pull request #19683 from tristang/require-option-for-counter-cache
Require explicit counter_cache option for has_many
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 9f6c832c1b..19cdd7f470 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -87,8 +87,15 @@ module ActiveRecord [association_scope.limit_value, count].compact.min end + + # Returns whether a counter cache should be used for this association. + # + # The counter_cache option must be given on either the owner or inverse + # association, and the column must be present on the owner. def has_cached_counter?(reflection = reflection()) - owner.attribute_present?(cached_counter_attribute_name(reflection)) + if reflection.options[:counter_cache] || (inverse = inverse_which_updates_counter_cache(reflection)) && inverse.options[:counter_cache] + owner.attribute_present?(cached_counter_attribute_name(reflection)) + end end def cached_counter_attribute_name(reflection = reflection()) |