diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2013-08-12 06:00:27 -0700 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2013-08-12 06:00:27 -0700 |
commit | 81acaaa02b3c5c99465e82308fd2f27405cca935 (patch) | |
tree | 5ceb2c20df8a3ff92f23d77d7217af290f696c3a /activerecord/lib | |
parent | 0f834868bfd0d98c6d144543bc49c86df53e9a91 (diff) | |
parent | cc368c3ab39bc376181d883c31877d9729fe3423 (diff) | |
download | rails-81acaaa02b3c5c99465e82308fd2f27405cca935.tar.gz rails-81acaaa02b3c5c99465e82308fd2f27405cca935.tar.bz2 rails-81acaaa02b3c5c99465e82308fd2f27405cca935.zip |
Merge pull request #11525 from Empact/nested_attributes_with_callbacks_bug
Improve #5476 - "Nested attributes with callbacks bugfix" to use add_to_target and clearer tests
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 8 | ||||
-rw-r--r-- | activerecord/lib/active_record/nested_attributes.rb | 10 |
2 files changed, 8 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 8ce02afef8..228c500f0a 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -290,7 +290,7 @@ module ActiveRecord # Returns true if the collection is empty. # - # If the collection has been loaded + # If the collection has been loaded # it is equivalent to <tt>collection.size.zero?</tt>. If the # collection has not been loaded, it is equivalent to # <tt>collection.exists?</tt>. If the collection has not already been @@ -366,8 +366,8 @@ module ActiveRecord target end - def add_to_target(record) - callback(:before_add, record) + def add_to_target(record, skip_callbacks = false) + callback(:before_add, record) unless skip_callbacks yield(record) if block_given? if association_scope.distinct_value && index = @target.index(record) @@ -376,7 +376,7 @@ module ActiveRecord @target << record end - callback(:after_add, record) + callback(:after_add, record) unless skip_callbacks set_inverse_instance(record) record diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index e53e8553ad..df28451bb7 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -465,19 +465,17 @@ 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) + unless 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 } - + # 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 - association.add_to_target(existing_record) + association.add_to_target(existing_record, :skip_callbacks) end - end - if !call_reject_if(association_name, attributes) assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy]) end else |