diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-26 10:27:13 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-26 10:27:13 -0300 |
commit | 031588ebe6b449bcba6e3becfaff6978ba10464a (patch) | |
tree | 179a789fd65ddc4c157f6bb088002a465755e6a8 /activerecord/lib | |
parent | e8003c7274c4049f409740b587e4e9e1f3df37f7 (diff) | |
parent | d730e374ca99a60b08c75aab7b0ed8a846a34924 (diff) | |
download | rails-031588ebe6b449bcba6e3becfaff6978ba10464a.tar.gz rails-031588ebe6b449bcba6e3becfaff6978ba10464a.tar.bz2 rails-031588ebe6b449bcba6e3becfaff6978ba10464a.zip |
Merge pull request #15754 from sgrif/sg-deprecate-hmt-counter-cache
Deprecate automatic counter caches on has_many :through
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 14 |
2 files changed, 23 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 477888228d..453615ba87 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -80,10 +80,14 @@ module ActiveRecord end def update_counter(difference, reflection = reflection()) + update_counter_in_database(difference, reflection) + update_counter_in_memory(difference, reflection) + end + + def update_counter_in_database(difference, reflection = reflection()) if has_cached_counter?(reflection) counter = cached_counter_attribute_name(reflection) owner.class.update_counters(owner.id, counter => difference) - update_counter_in_memory(difference, reflection) end end @@ -107,6 +111,10 @@ module ActiveRecord # Hence this method. def inverse_updates_counter_cache?(reflection = reflection()) counter_name = cached_counter_attribute_name(reflection) + inverse_updates_counter_named?(counter_name, reflection) + end + + def inverse_updates_counter_named?(counter_name, reflection = reflection()) reflection.klass._reflections.values.any? { |inverse_reflection| :belongs_to == inverse_reflection.macro && inverse_reflection.counter_cache_column == counter_name diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index af38f2f6dd..007e3bc555 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -63,6 +63,15 @@ module ActiveRecord end save_through_record(record) + if has_cached_counter? && !through_reflection_updates_counter_cache? + ActiveSupport::Deprecation.warn(<<-MESSAGE.strip_heredoc) + Automatic updating of counter caches on through associations has been + deprecated, and will be removed in Rails 5.0. Instead, please set the + appropriate counter_cache options on the has_many and belongs_to for + your associations to #{through_reflection.name}. + MESSAGE + update_counter_in_database(1) + end record end @@ -217,6 +226,11 @@ module ActiveRecord def invertible_for?(record) false end + + def through_reflection_updates_counter_cache? + counter_name = cached_counter_attribute_name + inverse_updates_counter_named?(counter_name, through_reflection) + end end end end |