aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2015-08-13 17:51:07 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2015-08-13 17:51:07 -0300
commit0168ce10a31ff2a00e2bce1c81ffbb25f0500281 (patch)
treec04467e96924fe3c69db9301c60a3f36c0d36ef8 /activerecord/lib
parent33dc653466c0058740611259956712a9027801b3 (diff)
parent543523045112c5f5920c486e6fcf2d7e1ffedf5a (diff)
downloadrails-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')
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb9
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())