diff options
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/association_proxy.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_one_association.rb | 9 |
2 files changed, 11 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 009e2ec4c2..91da7dec4d 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -34,6 +34,10 @@ module ActiveRecord end protected + def dependent? + @options[:dependent] || false + end + def quoted_record_ids(records) records.map { |record| record.quoted_id }.join(',') end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 30704b6f7e..e566089013 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -10,8 +10,13 @@ module ActiveRecord def replace(obj, dont_save = false) load_target unless @target.nil? - @target[@association_class_primary_key_name] = nil - @target.save unless @owner.new_record? + if dependent? && !dont_save + @target.destroy unless @target.new_record? + @owner.clear_association_cache + else + @target[@association_class_primary_key_name] = nil + @target.save unless @owner.new_record? + end end if obj.nil? |