has_and_belongs_to_many.rb 703 字节
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, :delete_sql, :insert_sql]
9
    end
10 11 12

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

    private

19
      def define_destroy_hook
20
        name = self.name
21 22 23
        model.send(:include, Module.new {
          class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def destroy_associations
24
              association(#{name.to_sym.inspect}).delete_all
25 26 27 28
              super
            end
          RUBY
        })
29 30 31
      end
  end
end