aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-11-08 10:19:09 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-11-08 10:19:09 +0000
commit35b4bdcff0e697765b562a74bd881e78de97f4d1 (patch)
tree15d1e68d1fd4fa2f4a4b40e133716b0cada078cf /activerecord/lib
parentc54b51fa7e6fd51f89cd74c9b2b2f144f2229ee9 (diff)
downloadrails-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-xactiverecord/lib/active_record/associations.rb13
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