has_many_through_association.rb 2.9 KB
Newer Older
1
require 'active_support/core_ext/object/blank'
2

3
module ActiveRecord
4
  # = Active Record Has Many Through Association
5
  module Associations
6
    class HasManyThroughAssociation < HasManyAssociation #:nodoc:
7
      include ThroughAssociation
8

9
      alias_method :new, :build
10

11 12 13
      # Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been
      # loaded and calling collection.size if it has. If it's more likely than not that the collection does
      # have a size larger than zero, and you need to fetch that collection afterwards, it'll take one fewer
14
      # SELECT query if you use #length.
15
      def size
16 17 18 19 20 21 22
        if has_cached_counter?
          @owner.send(:read_attribute, cached_counter_attribute_name)
        elsif loaded?
          @target.size
        else
          count
        end
23
      end
24

25 26 27 28 29 30 31 32 33 34 35
      def <<(*records)
        unless @owner.new_record?
          records.flatten.each do |record|
            raise_on_type_mismatch(record)
            record.save! if record.new_record?
          end
        end

        super
      end

36
      protected
37

38 39
        def insert_record(record, validate = true)
          return if record.new_record? && !record.save(:validate => validate)
40 41

          through_association = @owner.send(@reflection.through_reflection.name)
S
Santiago Pastorino 已提交
42
          through_association.create!(construct_join_attributes(record))
43 44

          update_counter(1)
45
          record
46 47
        end

48 49 50 51 52 53 54 55 56 57
      private

        def target_reflection_has_associated_record?
          if @reflection.through_reflection.macro == :belongs_to && @owner[@reflection.through_reflection.foreign_key].blank?
            false
          else
            true
          end
        end

58 59 60 61 62 63 64 65 66
        def update_through_counter?(method)
          case method
          when :destroy
            !inverse_updates_counter_cache?(@reflection.through_reflection)
          when :nullify
            false
          else
            true
          end
67
        end
68

69
        def delete_records(records, method)
70 71 72
          through = @owner.send(:association_proxy, @reflection.through_reflection.name)
          scope   = through.scoped.where(construct_join_attributes(*records))

73 74
          case method
          when :destroy
75
            count = scope.destroy_all.length
76
          when :nullify
77
            count = scope.update_all(@reflection.source_reflection.foreign_key => nil)
78
          else
79
            count = scope.delete_all
80
          end
81 82 83 84 85 86

          if @reflection.through_reflection.macro == :has_many && update_through_counter?(method)
            update_counter(-count, @reflection.through_reflection)
          end

          update_counter(-count)
87 88
        end

89
        def find_target
90
          return [] unless target_reflection_has_associated_record?
91
          scoped.all
92
        end
93

94
        # NOTE - not sure that we can actually cope with inverses here
95
        def invertible_for?(record)
96 97
          false
        end
98 99 100
    end
  end
end