aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorChris Thompson <chris@goclio.com>2013-03-07 14:43:00 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-04-30 16:14:16 -0700
commitebd7cc6f459e43aa03a6b8095266888909e0ee4d (patch)
treec64724fa000fc8a911236685650a4834270a187a /activerecord/lib
parente8e2f010afc29669e20b3f2461e1595b122220b8 (diff)
downloadrails-ebd7cc6f459e43aa03a6b8095266888909e0ee4d.tar.gz
rails-ebd7cc6f459e43aa03a6b8095266888909e0ee4d.tar.bz2
rails-ebd7cc6f459e43aa03a6b8095266888909e0ee4d.zip
Fix #8856 Ensure has_one association=(associate) triggers save.
activerecord/lib/active_record/associations.rb states: # [association=(associate)] # Assigns the associate object, extracts the primary key, sets it as the foreign key, # and saves the associate object. Since commit 42dd5d9f2976677a4bf22347f2dde1a8135dfbb4 to fix #7191, this is no longer the case if the associate has changed, but is the same object. For example: # Pirate has_one :ship pirate = Pirate.create!(catchphrase: "A Pirate") ship = pirate.build_ship(name: 'old name') ship.save! ship.name = 'new name' pirate.ship = ship That last line should trigger a save. Although we are not changing the association, the associate (ship) has changed.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb5
1 files changed, 2 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 98bd010f70..920038a543 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -25,9 +25,8 @@ module ActiveRecord
raise_on_type_mismatch!(record) if record
load_target
- # If target and record are nil, or target is equal to record,
- # we don't need to have transaction.
- if (target || record) && target != record
+ return self.target if !(target || record)
+ if (target != record) || record.changed?
transaction_if(save) do
remove_target!(options[:dependent]) if target && !target.destroyed?