has_and_belongs_to_many.rb 628 字节
Newer Older
1 2
module ActiveRecord::Associations::Builder
  class HasAndBelongsToMany < CollectionAssociation #:nodoc:
3 4 5
    def macro
      :has_and_belongs_to_many
    end
6

7
    def valid_options
8
      super + [:join_table, :association_foreign_key]
9
    end
10 11 12

    def build
      reflection = super
13
      define_destroy_hook
14 15 16
      reflection
    end

J
Jon Leighton 已提交
17 18 19 20 21
    def define_destroy_hook
      name = self.name
      model.send(:include, Module.new {
        class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def destroy_associations
22
            association(:#{name}).delete_all
J
Jon Leighton 已提交
23 24 25 26 27
            super
          end
        RUBY
      })
    end
28 29
  end
end