aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_many_through_association.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-15 09:19:54 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-26 07:24:34 -0600
commitd730e374ca99a60b08c75aab7b0ed8a846a34924 (patch)
tree179a789fd65ddc4c157f6bb088002a465755e6a8 /activerecord/lib/active_record/associations/has_many_through_association.rb
parente8003c7274c4049f409740b587e4e9e1f3df37f7 (diff)
downloadrails-d730e374ca99a60b08c75aab7b0ed8a846a34924.tar.gz
rails-d730e374ca99a60b08c75aab7b0ed8a846a34924.tar.bz2
rails-d730e374ca99a60b08c75aab7b0ed8a846a34924.zip
Deprecate automatic counter caches on has_many :through
Reliant on https://github.com/rails/rails/pull/15747 but pulled to a separate PR to reduce noise. `has_many :through` associations have the undocumented behavior of automatically detecting counter caches. However, the way in which it does so is inconsistent with counter caches everywhere else, and doesn't actually work consistently. As with normal `has_many` associations, the user should specify the counter cache on the `belongs_to`, if they'd like it updated.
Diffstat (limited to 'activerecord/lib/active_record/associations/has_many_through_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb14
1 files changed, 14 insertions, 0 deletions
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