From 3704088ebde5ef074d186bff0d380858a9a01055 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 29 Aug 2006 17:06:27 +0000 Subject: has_one supports the :dependent => :delete option which skips the typical callback chain and deletes the associated object directly from the database. Closes #5927. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4848 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 8f45ce5428..65db5f9b23 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -570,8 +570,9 @@ module ActiveRecord # sql fragment, such as "rank = 5". # * :order - 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" - # * :dependent - if set to :destroy (or true) all the associated objects are destroyed when this object is. Also, - # association is assigned. + # * :dependent - if set to :destroy (or true) the associated object is destroyed when this object is. If set to + # :delete the associated object is deleted *without* calling its destroy method. If set to :nullify the associated + # object's foreign key is set to NULL. Also, association is assigned. # * :foreign_key - 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. @@ -1020,12 +1021,14 @@ module ActiveRecord case reflection.options[:dependent] when :destroy, true module_eval "before_destroy '#{reflection.name}.destroy unless #{reflection.name}.nil?'" + when :delete + module_eval "before_destroy '#{reflection.class_name}.delete(#{reflection.name}.id) unless #{reflection.name}.nil?'" when :nullify module_eval "before_destroy '#{reflection.name}.update_attribute(\"#{reflection.primary_key_name}\", nil)'" when nil, false # pass else - raise ArgumentError, "The :dependent option expects either :destroy or :nullify." + raise ArgumentError, "The :dependent option expects either :destroy, :delete or :nullify." end end -- cgit v1.2.3