From c01be9de322ba846923340e41e69821d01541610 Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Sat, 11 Jul 2009 20:04:18 +0200 Subject: Fix has_one with foreign_key and primary_key association bug which caused the associated object being lost when saving the owner. [#1756 state:resolved] Mixed in a bit from patch by ransom-briggs. [#2813 state:resolved] Signed-off-by: Eloy Duran --- activerecord/lib/active_record/autosave_association.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 10dd0b4f05..75c49ecb2b 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -314,9 +314,12 @@ module ActiveRecord if autosave && association.marked_for_destruction? association.destroy - elsif autosave != false && (new_record? || association.new_record? || association[reflection.primary_key_name] != id || autosave) - association[reflection.primary_key_name] = id - association.save(!autosave) + else + key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id + if autosave != false && (new_record? || association.new_record? || association[reflection.primary_key_name] != key || autosave) + association[reflection.primary_key_name] = key + association.save(!autosave) + end end end end -- cgit v1.2.3