Sha256: 57ae8f4ad7025ddb4a2ae5ec4842f0c608b4bcb3066333419696ab50857d380d

Contents?: true

Size: 1.96 KB

Versions: 5

Compression:

Stored size: 1.96 KB

Contents

module ActiveRecord
  # = Active Record Has And Belongs To Many Association
  module Associations
    class HasAndBelongsToManyAssociation < CollectionAssociation #:nodoc:
      attr_reader :join_table

      def initialize(owner, reflection)
        @join_table = Arel::Table.new(reflection.options[:join_table])
        super
      end

      def insert_record(record, validate = true, raise = false)
        if record.new_record?
          if raise
            record.save!(:validate => validate)
          else
            return unless record.save(:validate => validate)
          end
        end

        if options[:insert_sql]
          owner.connection.insert(interpolate(options[:insert_sql], record))
        else
          stmt = join_table.compile_insert(
            join_table[reflection.foreign_key]             => owner.id,
            join_table[reflection.association_foreign_key] => record.id
          )

          owner.connection.insert stmt
        end

        record
      end

      # ActiveRecord::Relation#delete_all needs to support joins before we can use a
      # SQL-only implementation.
      alias delete_all_on_destroy delete_all

      private

        def count_records
          load_target.size
        end

        def delete_records(records, method)
          if sql = options[:delete_sql]
            records = load_target if records == :all
            records.each { |record| owner.connection.delete(interpolate(sql, record)) }
          else
            relation  = join_table
            condition = relation[reflection.foreign_key].eq(owner.id)

            unless records == :all
              condition = condition.and(
                relation[reflection.association_foreign_key].
                  in(records.map { |x| x.id }.compact)
              )
            end

            owner.connection.delete(relation.where(condition).compile_delete)
          end
        end

        def invertible_for?(record)
          false
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
challah-0.6.2 vendor/bundle/gems/activerecord-3.2.5/lib/active_record/associations/has_and_belongs_to_many_association.rb
challah-0.6.1 vendor/bundle/gems/activerecord-3.2.5/lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.5 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.4 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.4.rc1 lib/active_record/associations/has_and_belongs_to_many_association.rb