has_many_through_association.rb 5.7 KB
Newer Older
1

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

8 9
      def initialize(owner, reflection)
        super
J
Jon Leighton 已提交
10 11 12

        @through_records     = {}
        @through_association = nil
13 14
      end

15 16 17
      # 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
18
      # SELECT query if you use #length.
19
      def size
20
        if has_cached_counter?
21
          owner.send(:read_attribute, cached_counter_attribute_name)
22
        elsif loaded?
23
          target.size
24 25 26
        else
          count
        end
27
      end
28

29
      def concat(*records)
30
        unless owner.new_record?
31 32 33 34 35 36 37
          records.flatten.each do |record|
            raise_on_type_mismatch(record)
            record.save! if record.new_record?
          end
        end

        super
38
      end
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53
      def concat_records(records)
        ensure_not_nested

        records = super

        if owner.new_record? && records
          records.flatten.each do |record|
            build_through_record(record)
          end
        end

        records
      end

54
      def insert_record(record, validate = true, raise = false)
55
        ensure_not_nested
56 57 58 59 60 61 62 63

        if record.new_record?
          if raise
            record.save!(:validate => validate)
          else
            return unless record.save(:validate => validate)
          end
        end
64

65
        save_through_record(record)
66 67 68
        update_counter(1)
        record
      end
69

70 71
      private

72
        def through_association
J
Jon Leighton 已提交
73
          @through_association ||= owner.association(through_reflection.name)
74
        end
75

76 77 78 79 80 81 82 83 84
        # We temporarily cache through record that has been build, because if we build a
        # through record in build_record and then subsequently call insert_record, then we
        # want to use the exact same object.
        #
        # However, after insert_record has been called, we clear the cache entry because
        # we want it to be possible to have multiple instances of the same record in an
        # association
        def build_through_record(record)
          @through_records[record.object_id] ||= begin
85 86 87
            ensure_mutable

            through_record = through_association.build
88
            through_record.send("#{source_reflection.name}=", record)
89
            through_record
90
          end
91
        end
92

93 94 95 96 97 98
        def save_through_record(record)
          build_through_record(record).save!
        ensure
          @through_records.delete(record.object_id)
        end

99
        def build_record(attributes)
100
          ensure_not_nested
101

102
          record = super(attributes)
103

104
          inverse = source_reflection.inverse_of
105 106
          if inverse
            if inverse.macro == :has_many
107
              record.send(inverse.name) << build_through_record(record)
108
            elsif inverse.macro == :has_one
109
              record.send("#{inverse.name}=", build_through_record(record))
110
            end
111
          end
112 113

          record
114 115
        end

116
        def target_reflection_has_associated_record?
117
          !(through_reflection.macro == :belongs_to && owner[through_reflection.foreign_key].blank?)
118 119
        end

120 121 122
        def update_through_counter?(method)
          case method
          when :destroy
123
            !inverse_updates_counter_cache?(through_reflection)
124 125 126 127 128
          when :nullify
            false
          else
            true
          end
129
        end
130

131
        def delete_records(records, method)
132
          ensure_not_nested
J
Jon Leighton 已提交
133

134 135 136 137
          # This is unoptimised; it will load all the target records
          # even when we just want to delete everything.
          records = load_target if records == :all

J
Jon Leighton 已提交
138
          scope = through_association.scope
139
          scope.where! construct_join_attributes(*records)
140

141 142
          case method
          when :destroy
143
            count = scope.destroy_all.length
144
          when :nullify
145
            count = scope.update_all(source_reflection.foreign_key => nil)
146
          else
147
            count = scope.delete_all
148 149
          end

J
Jon Leighton 已提交
150
          delete_through_records(records)
J
Jon Leighton 已提交
151

152 153 154 155 156
          if source_reflection.options[:counter_cache]
            counter = source_reflection.counter_cache_column
            klass.decrement_counter counter, records.map(&:id)
          end

157 158
          if through_reflection.macro == :has_many && update_through_counter?(method)
            update_counter(-count, through_reflection)
159 160
          end

161
          update_counter(-count)
162
        end
163

164 165 166 167 168 169
        def through_records_for(record)
          attributes = construct_join_attributes(record)
          candidates = Array.wrap(through_association.target)
          candidates.find_all { |c| c.attributes.slice(*attributes.keys) == attributes }
        end

J
Jon Leighton 已提交
170
        def delete_through_records(records)
171
          records.each do |record|
172
            through_records = through_records_for(record)
173 174

            if through_reflection.macro == :has_many
J
Jon Leighton 已提交
175
              through_records.each { |r| through_association.target.delete(r) }
176
            else
J
Jon Leighton 已提交
177 178 179
              if through_records.include?(through_association.target)
                through_association.target = nil
              end
180
            end
181 182

            @through_records.delete(record.object_id)
183
          end
184 185
        end

186
        def find_target
187
          return [] unless target_reflection_has_associated_record?
J
Jon Leighton 已提交
188
          scope.to_a
189
        end
190 191

        # NOTE - not sure that we can actually cope with inverses here
192
        def invertible_for?(record)
193 194
          false
        end
195 196 197
    end
  end
end