aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-07-19 15:52:45 -0600
committerSean Griffin <sean@thoughtbot.com>2015-07-19 15:52:45 -0600
commit6765d4874e7afaa8da0c7790710fe43726ef412c (patch)
treef6d73b4779bed275d29f91c2301047534e122fb6 /activerecord/lib/active_record
parent0bd247cc7679adb3c2e30b46ecc9afd57ac46c45 (diff)
parent8cd1d5a41cf54f0dfde2b2d0d406457a6a58dbeb (diff)
downloadrails-6765d4874e7afaa8da0c7790710fe43726ef412c.tar.gz
rails-6765d4874e7afaa8da0c7790710fe43726ef412c.tar.bz2
rails-6765d4874e7afaa8da0c7790710fe43726ef412c.zip
Merge Pull Request #16445
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb16
1 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 97eb007f62..6e4a53f7fb 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -33,16 +33,24 @@ module ActiveRecord::Associations::Builder
if (@_after_create_counter_called ||= false)
@_after_create_counter_called = false
- elsif attribute_changed?(foreign_key) && !new_record? && reflection.constructable?
- model = reflection.klass
+ elsif attribute_changed?(foreign_key) && !new_record?
+ if reflection.polymorphic?
+ model = attribute(reflection.foreign_type).try(:constantize)
+ model_was = attribute_was(reflection.foreign_type).try(:constantize)
+ else
+ model = reflection.klass
+ model_was = reflection.klass
+ end
+
foreign_key_was = attribute_was foreign_key
foreign_key = attribute foreign_key
if foreign_key && model.respond_to?(:increment_counter)
model.increment_counter(cache_column, foreign_key)
end
- if foreign_key_was && model.respond_to?(:decrement_counter)
- model.decrement_counter(cache_column, foreign_key_was)
+
+ if foreign_key_was && model_was.respond_to?(:decrement_counter)
+ model_was.decrement_counter(cache_column, foreign_key_was)
end
end
end