diff options
author | Eileen M. Uchitelle <eileencodes@users.noreply.github.com> | 2016-12-27 10:41:26 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-27 10:41:26 -0500 |
commit | fb6ac0c5f3227d67d11c89fa7398fbfe183bfb30 (patch) | |
tree | e8ecaa6f54a75a61da3c7423c3c0f9b8c576f2ac /activerecord/lib/active_record | |
parent | 4d5ced349ca2b4fe756481f4b01a7a4574457889 (diff) | |
parent | 9eee7822ac4bce983ad45a98c4d111eb36285199 (diff) | |
download | rails-fb6ac0c5f3227d67d11c89fa7398fbfe183bfb30.tar.gz rails-fb6ac0c5f3227d67d11c89fa7398fbfe183bfb30.tar.bz2 rails-fb6ac0c5f3227d67d11c89fa7398fbfe183bfb30.zip |
Merge pull request #27442 from kamipo/fix_27434
Add a record to target before any callbacks loads the record
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 25 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 4 |
2 files changed, 16 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 46923f690a..13f77c7d4d 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -299,12 +299,23 @@ module ActiveRecord def replace_on_target(record, index, skip_callbacks) callback(:before_add, record) unless skip_callbacks - yield(record) if block_given? + begin + if index + record_was = target[index] + target[index] = record + else + target << record + end - if index - @target[index] = record - else - append_record(record) + yield(record) if block_given? + rescue + if index + target[index] = record_was + else + target.delete(record) + end + + raise end callback(:after_add, record) unless skip_callbacks @@ -502,10 +513,6 @@ module ActiveRecord load_target.select { |r| ids.include?(r.id.to_s) } end end - - def append_record(record) - @target << record unless @target.include?(record) - end end end end diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 8c90aea975..0c0aefe3b9 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -206,10 +206,6 @@ module ActiveRecord def invertible_for?(record) false end - - def append_record(record) - @target << record - end end end end |