1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Avoid empty transaction from setting has_one association on new record.

This commit is contained in:
Dylan Thacker-Smith 2013-09-04 18:36:28 -04:00
parent 233c6d4c29
commit a94e2db05c
3 changed files with 9 additions and 1 deletions

View file

@ -1,3 +1,8 @@
* Setting a has_one association on a new record no longer causes an empty
transaction.
*Dylan Thacker-Smith*
* Re-use `order` argument pre-processing for `reorder`.
*Paul Nikitochkin*

View file

@ -27,6 +27,7 @@ module ActiveRecord
return self.target if !(target || record)
if (target != record) || record.changed?
save &&= owner.persisted?
transaction_if(save) do
remove_target!(options[:dependent]) if target && !target.destroyed?
@ -34,7 +35,7 @@ module ActiveRecord
set_owner_attributes(record)
set_inverse_instance(record)
if owner.persisted? && save && !record.save
if save && !record.save
nullify_owner_attributes(record)
set_owner_attributes(target) if target
raise RecordNotSaved, "Failed to save the new associated #{reflection.name}."

View file

@ -505,6 +505,8 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_no_queries { company.account = nil }
account = Account.find(2)
assert_queries { company.account = account }
assert_no_queries { Firm.new.account = account }
end
def test_has_one_assignment_triggers_save_on_change