Sha256: ad0f7180aa5085233dbea67d6b69b889cd268a71be09242ea52daf55a4abf7fd

Contents?: true

Size: 1.84 KB

Versions: 85

Compression:

Stored size: 1.84 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
            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
            owner.connection.delete stmt
          end
        end

        def invertible_for?(record)
          false
        end
    end
  end
end

Version data entries

85 entries across 71 versions & 13 rubygems

Version Path
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/activerecord-3.2.22.5/lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.22.5 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.22.4 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.22.3 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.22.2 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.22.1 lib/active_record/associations/has_and_belongs_to_many_association.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.22 lib/active_record/associations/has_and_belongs_to_many_association.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.12/lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.21 lib/active_record/associations/has_and_belongs_to_many_association.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/associations/has_and_belongs_to_many_association.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/has_and_belongs_to_many_association.rb
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/has_and_belongs_to_many_association.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/associations/has_and_belongs_to_many_association.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/associations/has_and_belongs_to_many_association.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.20 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.19 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.18 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.17 lib/active_record/associations/has_and_belongs_to_many_association.rb