aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorDr.(USA) Joerg Schray <joerg.schray@tandem-softworks.de>2012-03-16 12:26:24 +0100
committerBen Woosley <ben.woosley@gmail.com>2013-08-12 02:37:39 -0700
commit018697dece967f3f2861a085e747ba14f06c507c (patch)
tree0255f2b86bc10e6295ab5368cdcb9a45c4b2a587 /activerecord/lib/active_record
parent0f834868bfd0d98c6d144543bc49c86df53e9a91 (diff)
downloadrails-018697dece967f3f2861a085e747ba14f06c507c.tar.gz
rails-018697dece967f3f2861a085e747ba14f06c507c.tar.bz2
rails-018697dece967f3f2861a085e747ba14f06c507c.zip
Fix interactions between :before_add callbacks and nested attributes assignment
Issue #1: :before_add callback is called when nested attributes assignment assigns to existing record if the association is not yet loaded Issue #2: Nested Attributes assignment does not affect the record in the association target when callback triggers loading of the association
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/nested_attributes.rb19
1 files changed, 9 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb
index e53e8553ad..403ad98c3b 100644
--- a/activerecord/lib/active_record/nested_attributes.rb
+++ b/activerecord/lib/active_record/nested_attributes.rb
@@ -465,16 +465,15 @@ module ActiveRecord
association.build(attributes.except(*UNASSIGNABLE_KEYS))
end
elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s }
- 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.target.detect { |record| record == existing_record }
-
- if target_record
- existing_record = target_record
- else
- association.add_to_target(existing_record)
- end
+ # 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)
+ # Take into account that the proxy_target may have changed due to callbacks
+ target_record = association.target.detect { |record| record.id.to_s == attributes['id'].to_s }
+ if target_record
+ existing_record = target_record
+ else
+ #FIXME: there is no good way of adding the record without callback
+ association.target << existing_record
end
if !call_reject_if(association_name, attributes)