Permalink
Browse files

Merge pull request #27442 from kamipo/fix_27434

Add a record to target before any callbacks loads the record
  • Loading branch information...
eileencodes authored and kaspth committed Dec 27, 2016
1 parent 754a67b commit 70f13384270e9609de200b0b29e82ce515487d24
@@ -430,12 +430,23 @@ def add_to_target(record, skip_callbacks = false, &block)
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
@@ -658,10 +669,6 @@ def first_nth_or_last(type, *args)
set_inverse_instance record if record.is_a? ActiveRecord::Base
end
end
-
- def append_record(record)
- @target << record unless @target.include?(record)
- end
end
end
end
@@ -205,10 +205,6 @@ def find_target
def invertible_for?(record)
false
end
-
- def append_record(record)
- @target << record
- end
end
end
end
@@ -2392,7 +2392,7 @@ def self.name
test "double insertion of new object to association when same association used in the after create callback of a new object" do
reset_callbacks(:save, Bulb) do
- Bulb.after_save { |record| record.car.bulbs.to_a }
+ Bulb.after_save { |record| record.car.bulbs.load }
car = Car.create!
car.bulbs << Bulb.new
assert_equal 1, car.bulbs.size

0 comments on commit 70f1338

Please sign in to comment.