Sha256: 7a041eda5b311478ba2385a145060132536f34b0c1abd98e66230b39b0fdf65f

Contents?: true

Size: 1.81 KB

Versions: 14

Compression:

Stored size: 1.81 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.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.class.connection.insert stmt
        end

        record
      end

      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.class.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.class.connection.delete(relation.where(condition).compile_delete)
          end
        end

        def invertible_for?(record)
          false
        end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
activerecord-4.0.5 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.4 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.4.rc1 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.3 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.2 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.1 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.1.rc4 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.1.rc3 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.1.rc2 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.1.rc1 lib/active_record/associations/has_and_belongs_to_many_association.rb
challah-1.0.0 vendor/bundle/gems/activerecord-4.0.0/lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.0 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.0.rc2 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-4.0.0.rc1 lib/active_record/associations/has_and_belongs_to_many_association.rb