aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2015-11-23 16:22:03 +0200
committerBogdan Gusiev <agresso@gmail.com>2015-11-23 16:22:03 +0200
commit6d0d83a33f59d9415685852cf77818c41e2e2700 (patch)
treecd201839897deccfc6c8ee310c292c6ec4210dcb /activerecord/lib/active_record
parent4547e894e9f924221f1ec4ff8e71fa750bab3595 (diff)
downloadrails-6d0d83a33f59d9415685852cf77818c41e2e2700.tar.gz
rails-6d0d83a33f59d9415685852cf77818c41e2e2700.tar.bz2
rails-6d0d83a33f59d9415685852cf77818c41e2e2700.zip
Bugfix collection association #create method …
When same association is loaded in the model creation callback The new object is inserted into association twice
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb12
1 files changed, 8 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index f32dddb8f0..473b80a658 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -414,12 +414,16 @@ module ActiveRecord
def replace_on_target(record, index, skip_callbacks)
callback(:before_add, record) unless skip_callbacks
+
+ was_loaded = loaded?
yield(record) if block_given?
- if index
- @target[index] = record
- else
- @target << record
+ unless !was_loaded && loaded?
+ if index
+ @target[index] = record
+ else
+ @target << record
+ end
end
callback(:after_add, record) unless skip_callbacks