Sha256: 39945a522751845fb6923c965f320f01cc66fc5b96d83669c9e8ae9ddeff1107

Contents?: true

Size: 1.87 KB

Versions: 15

Compression:

Stored size: 1.87 KB

Contents

module ActiveRecord
  module Associations
    class HasAndBelongsToManyAssociation
      def insert_record(record, validate = true)
        return if record.new_record? && !record.save(:validate => validate)

        if options[:insert_sql]
          owner.connection.insert(interpolate(options[:insert_sql], record))
        else
          # CPK
          #stmt = join_table.compile_insert(
          #  join_table[reflection.foreign_key]             => owner.id,
          #  join_table[reflection.association_foreign_key] => record.id
          #)
          join_values = Hash.new
          Array(reflection.foreign_key).zip(Array(owner.id)) do |name, value|
            attribute = join_table[name]
            join_values[attribute] = value
          end

          Array(reflection.association_foreign_key).zip(Array(record.id)) do |name, value|
            attribute = join_table[name]
            join_values[attribute] = value
          end

          stmt = join_table.compile_insert(join_values)

          owner.connection.insert stmt.to_sql
        end

        record
      end

      def delete_records(records, method)
        if sql = options[:delete_sql]
          records.each { |record| owner.connection.delete(interpolate(sql, record)) }
        else
          relation = join_table
          # CPK
          # stmt = relation.where(relation[reflection.foreign_key].eq(owner.id).
          #  and(relation[reflection.association_foreign_key].in(records.map { |x| x.id }.compact))
          #).compile_delete

          predicate1 = cpk_id_predicate(relation, Array(reflection.foreign_key), Array(owner.id))
          predicate2 = cpk_in_predicate(relation, Array(reflection.association_foreign_key), records.map { |x| x.id })
          stmt = relation.where(predicate1.and(predicate2)).compile_delete

          owner.connection.delete stmt.to_sql
        end
      end
    end    
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
composite_primary_keys-5.0.1 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-5.0.0 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-5.0.0.rc1 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.1.2 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.1.1 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0.beta9 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0.beta8 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0.beta7 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0.beta6 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0.beta5 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0.beta4 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0.beta3 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0.beta2 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
composite_primary_keys-4.0.0.beta1 lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb