diff options
author | Tomas D'Stefano <tomasdestefi@gmail.com> | 2011-05-31 11:43:19 -0300 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-07-08 22:26:54 +0100 |
commit | ea4b94a7bf707d27af44701fa44ef5f916b84e01 (patch) | |
tree | ae5ef07b2cccc9f5dc0d8a1c0f453fe4123dd9ff /activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb | |
parent | dfec3737b05d22e6f298bcae0a8948a4b8803ebd (diff) | |
download | rails-ea4b94a7bf707d27af44701fa44ef5f916b84e01.tar.gz rails-ea4b94a7bf707d27af44701fa44ef5f916b84e01.tar.bz2 rails-ea4b94a7bf707d27af44701fa44ef5f916b84e01.zip |
Destroy association habtm record before destroying the record itself. Fixes issue #402.
Diffstat (limited to 'activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb index d7632b2ea9..30fc44b4c2 100644 --- a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb +++ b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb @@ -7,24 +7,22 @@ module ActiveRecord::Associations::Builder def build reflection = super check_validity(reflection) - define_after_destroy_method + define_destroy_hook reflection end private - def define_after_destroy_method + def define_destroy_hook name = self.name - model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1) - def #{after_destroy_method_name} - association(#{name.to_sym.inspect}).delete_all - end - eoruby - model.after_destroy after_destroy_method_name - end - - def after_destroy_method_name - "has_and_belongs_to_many_after_destroy_for_#{name}" + model.send(:include, Module.new { + class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def destroy_associations + association(#{name.to_sym.inspect}).delete_all + super + end + RUBY + }) end # TODO: These checks should probably be moved into the Reflection, and we should not be |