aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/nested_attributes.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-12-22 00:19:59 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2010-12-23 15:19:17 -0800
commitff7bde62c857ec94f45a5be3bc76468deb8b0b3a (patch)
tree64bb289a46f3de6216748aae5b1d2fd2d253f8a6 /activerecord/lib/active_record/nested_attributes.rb
parent030480ac1f4fbf8bf74a0d9298544426caf26894 (diff)
downloadrails-ff7bde62c857ec94f45a5be3bc76468deb8b0b3a.tar.gz
rails-ff7bde62c857ec94f45a5be3bc76468deb8b0b3a.tar.bz2
rails-ff7bde62c857ec94f45a5be3bc76468deb8b0b3a.zip
When a has_many association is not :uniq, appending the same record multiple times should append it to the @target multiple times [#5964 state:resolved]
Diffstat (limited to 'activerecord/lib/active_record/nested_attributes.rb')
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb13
1 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index 050b521b6a..16023defe3 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -405,7 +405,18 @@ module ActiveRecord
end
elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s }
- association.send(:add_record_to_target_with_callbacks, existing_record) if !association.loaded? && !call_reject_if(association_name, attributes)
+ unless association.loaded? || call_reject_if(association_name, attributes)
+ # Make sure we are operating on the actual object which is in the association's
+ # proxy_target array (either by finding it, or adding it if not found)
+ target_record = association.proxy_target.detect { |record| record == existing_record }
+
+ if target_record
+ existing_record = target_record
+ else
+ association.send(:add_record_to_target_with_callbacks, existing_record)
+ end
+ end
+
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
else