diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-03-06 13:40:21 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-03-06 13:40:21 +0000 |
commit | 7267db58906434da54e0f50122b5e8b704b1c953 (patch) | |
tree | 089ebe97424e2fab2630f629c7a5af1a21d72057 /activerecord/lib | |
parent | 838ae35d63c34872d46bee8b006796ebdd9c7722 (diff) | |
download | rails-7267db58906434da54e0f50122b5e8b704b1c953.tar.gz rails-7267db58906434da54e0f50122b5e8b704b1c953.tar.bz2 rails-7267db58906434da54e0f50122b5e8b704b1c953.zip |
Added destruction of dependent objects in has_one associations when a new assignment happens #742 [mindel]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@843 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
3 files changed, 13 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 381f3e4931..229cfb5cfa 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -270,7 +270,8 @@ module ActiveRecord # sql fragment, such as "rank = 5". # * <tt>:order</tt> - specify the order from which the associated object will be picked at the top. Specified as # an "ORDER BY" sql fragment, such as "last_name, first_name DESC" - # * <tt>:dependent</tt> - if set to true the associated object is destroyed alongside this object + # * <tt>:dependent</tt> - if set to true, the associated object is destroyed when this object is. It's also destroyed if another + # association is assigned. # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name # of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_one association will use "person_id" # as the default foreign_key. 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? |