diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2005-11-08 10:19:09 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2005-11-08 10:19:09 +0000 |
commit | 35b4bdcff0e697765b562a74bd881e78de97f4d1 (patch) | |
tree | 15d1e68d1fd4fa2f4a4b40e133716b0cada078cf /activerecord/lib | |
parent | c54b51fa7e6fd51f89cd74c9b2b2f144f2229ee9 (diff) | |
download | rails-35b4bdcff0e697765b562a74bd881e78de97f4d1.tar.gz rails-35b4bdcff0e697765b562a74bd881e78de97f4d1.tar.bz2 rails-35b4bdcff0e697765b562a74bd881e78de97f4d1.zip |
Destroy associated has_and_belongs_to_many records after all before_destroy callbacks but before destroy. This allows you to act on the habtm association as you please while preserving referential integrity. Closes #2065.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2940 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 077245559a..a14aed1700 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -652,8 +652,17 @@ module ActiveRecord collection_accessor_methods(association_name, association_class_name, association_class_primary_key_name, options, HasAndBelongsToManyAssociation) - before_destroy_sql = "DELETE FROM #{options[:join_table]} WHERE #{association_class_primary_key_name} = \\\#{self.quoted_id}" - module_eval(%{before_destroy "self.connection.delete(%{#{before_destroy_sql}})"}) # " + # Don't use a before_destroy callback since users' before_destroy + # callbacks will be executed after the association is wiped out. + old_method = "destroy_without_habtm_shim_for_#{association_name}" + class_eval <<-end_eval + alias_method :#{old_method}, :destroy_without_callbacks + def destroy_without_callbacks + #{association_name}.clear + #{old_method} + end + end_eval + add_association_callbacks(association_name, options) # deprecated api |