提交 3f16a098 编写于 作者: S Sean Griffin

Merge pull request #18155 from bogdan/collection_association_double_element_fix

Bugfix collection association #create method
......@@ -414,12 +414,16 @@ def add_to_target(record, skip_callbacks = false, &block)
def replace_on_target(record, index, skip_callbacks)
callback(:before_add, record) unless skip_callbacks
was_loaded = loaded?
yield(record) if block_given?
if index
@target[index] = record
else
@target << record
unless !was_loaded && loaded?
if index
@target[index] = record
else
@target << record
end
end
callback(:after_add, record) unless skip_callbacks
......
......@@ -2348,6 +2348,12 @@ def self.name
assert_equal [first_bulb, second_bulb], car.bulbs
end
test 'double insertion of new object to association when same association used in the after create callback of a new object' do
car = Car.create!
car.bulbs << TrickyBulb.new
assert_equal 1, car.bulbs.size
end
def test_association_force_reload_with_only_true_is_deprecated
company = Company.find(1)
......
......@@ -50,3 +50,9 @@ class FailedBulb < Bulb
throw(:abort)
end
end
class TrickyBulb < Bulb
after_create do |record|
record.car.bulbs.to_a
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册