From bf28422cbda9068afb900020fa6911afd01963e1 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 11 Jun 2013 12:01:29 -0700 Subject: [PATCH] check whether the association is constructible rather than checking constants --- .../lib/active_record/associations/builder/belongs_to.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index aaef86d5b9e6d..783f4e857a6f3 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -55,10 +55,10 @@ def belongs_to_counter_cache_after_update(association, reflection) if (@_after_create_counter_called ||= false) @_after_create_counter_called = false - elsif attribute_changed?(foreign_key) && !new_record? && Object.const_defined?(name.to_s.camelize) - model = name.to_s.camelize.constantize + elsif attribute_changed?(foreign_key) && !new_record? && association.constructable? + model = reflection.klass foreign_key_was = attribute_was foreign_key - foreign_key = attribute foreign_key + foreign_key = attribute foreign_key if foreign_key && model.respond_to?(:increment_counter) model.increment_counter(cache_column, foreign_key)