aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-11-11 02:26:23 -0800
committerYves Senn <yves.senn@gmail.com>2013-11-11 02:26:23 -0800
commitf5802708db9480624b611975ef190dcc1b9443c5 (patch)
tree1417700fccb3e871c68d1cf5224e56965b2092c9 /activerecord/lib/active_record
parent9222928dfb8aee5da2266b36ab7cfff8789f7022 (diff)
parent3ed5642e69b9a132c78bc0b1e54cbdc3753d8a94 (diff)
downloadrails-f5802708db9480624b611975ef190dcc1b9443c5.tar.gz
rails-f5802708db9480624b611975ef190dcc1b9443c5.tar.bz2
rails-f5802708db9480624b611975ef190dcc1b9443c5.zip
Merge pull request #12834 from dredozubov/has_one_association_replacement
Fixes problem with replacing has_one association record with itself
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb6
1 files changed, 4 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 0008600418..944caacab6 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -26,11 +26,13 @@ module ActiveRecord
load_target
return self.target if !(target || record)
- if (target != record) || record.changed?
+
+ assigning_another_record = target != record
+ if assigning_another_record || record.changed?
save &&= owner.persisted?
transaction_if(save) do
- remove_target!(options[:dependent]) if target && !target.destroyed?
+ remove_target!(options[:dependent]) if target && !target.destroyed? && assigning_another_record
if record
set_owner_attributes(record)