未验证 提交 3053e544 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #38942 from joshmn/joshmn_fix_has_one_touch

Prevent has_one from touching parent record unless persisted
* Prevent `build_association` from `touching` a parent record if the record isn't persisted for `has_one` associations.
Fixes #38219
*Josh Brody*
* Add support for `if_not_exists` option for adding index.
The `add_index` method respects `if_not_exists` option. If it is set to true
......
......@@ -81,7 +81,9 @@ def remove_target!(method)
target.delete
when :destroy
target.destroyed_by_association = reflection
target.destroy
if target.persisted?
target.destroy
end
else
nullify_owner_attributes(target)
remove_inverse_instance(target)
......
......@@ -851,4 +851,22 @@ def test_dependency_should_halt_parent_destruction
assert_not author.destroy
end
end
class SpecialCar < ActiveRecord::Base
self.table_name = "cars"
has_one :special_bulb, inverse_of: :car, dependent: :destroy, class_name: "SpecialBulb", foreign_key: "car_id"
end
class SpecialBulb < ActiveRecord::Base
self.table_name = "bulbs"
belongs_to :car, inverse_of: :special_bulb, touch: true, class_name: "SpecialCar"
end
def test_has_one_with_touch_option_on_nonpersisted_built_associations_doesnt_update_parent
car = SpecialCar.create(name: "honda")
assert_queries(1) do
car.build_special_bulb
car.build_special_bulb
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册