Permalink
Browse files

extract a method for updating without a record object

  • Loading branch information...
tenderlove committed Dec 12, 2013
1 parent 9798a11 commit 45940e7112cab8414b15baeb4ef5783247e9f576
Showing with 10 additions and 6 deletions.
  1. +10 −6 activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -15,7 +15,7 @@ def replace(record)
set_inverse_instance(record)
@updated = true
else
- update_counters(record)
+ update_counters_without_record
remove_keys
end
@@ -42,11 +42,15 @@ def update_counters(record)
return unless counter_cache_name && owner.persisted?
- if record
- update_with_record record, counter_cache_name
- else
- update_without_record counter_cache_name
- end
+ update_with_record record, counter_cache_name
+ end
+
+ def update_counters_without_record
+ counter_cache_name = reflection.counter_cache_column
+
+ return unless counter_cache_name && owner.persisted?
+
+ update_without_record counter_cache_name
end
def update_with_record record, counter_cache_name

0 comments on commit 45940e7

Please sign in to comment.