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
activerecord-3.2.16 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.15 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.15.rc3 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.15.rc2 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.15.rc1 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.14 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.14.rc2 lib/active_record/associations/has_and_belongs_to_many_association.rb
activerecord-3.2.14.rc1 lib/active_record/associations/has_and_belongs_to_many_association.rb
swipe-rails-0.0.5 vendor/bundle/gems/activerecord-3.2.13/lib/active_record/associations/has_and_belongs_to_many_association.rb
active_mailer-0.0.9 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
active_mailer-0.0.8 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
active_mailer-0.0.7 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
active_mailer-0.0.6 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
challah-1.0.0.beta3 vendor/bundle/gems/activerecord-3.2.13/lib/active_record/associations/has_and_belongs_to_many_association.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/has_and_belongs_to_many_association.rb
challah-1.0.0.beta2 vendor/bundle/gems/activerecord-3.2.13/lib/active_record/associations/has_and_belongs_to_many_association.rb
challah-1.0.0.beta vendor/bundle/gems/activerecord-3.2.13/lib/active_record/associations/has_and_belongs_to_many_association.rb
challah-1.0.0.beta vendor/bundle/gems/activerecord-3.2.11/lib/active_record/associations/has_and_belongs_to_many_association.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/has_and_belongs_to_many_association.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.13/lib/active_record/associations/has_and_belongs_to_many_association.rb