has_many_through_association.rb 5.4 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 10
      def initialize(owner, reflection)
        super
J
Jon Leighton 已提交
11 12 13

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

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

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

        super
39
      end
40

41
      def insert_record(record, validate = true, raise = false)
42
        ensure_not_nested
43 44 45 46 47 48 49 50

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

52
        save_through_record(record)
53 54 55
        update_counter(1)
        record
      end
56

57 58 59 60
      # ActiveRecord::Relation#delete_all needs to support joins before we can use a
      # SQL-only implementation.
      alias delete_all_on_destroy delete_all

61 62
      private

63
        def through_association
J
Jon Leighton 已提交
64
          @through_association ||= owner.association(through_reflection.name)
65
        end
66

67 68 69 70 71 72 73 74 75
        # 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
76 77 78
            ensure_mutable

            through_record = through_association.build
79
            through_record.send("#{source_reflection.name}=", record)
80
            through_record
81
          end
82
        end
83

84 85 86 87 88 89
        def save_through_record(record)
          build_through_record(record).save!
        ensure
          @through_records.delete(record.object_id)
        end

90
        def build_record(attributes, options = {})
91
          ensure_not_nested
92

93
          record = super(attributes, options)
94

95
          inverse = source_reflection.inverse_of
96 97
          if inverse
            if inverse.macro == :has_many
98
              record.send(inverse.name) << build_through_record(record)
99
            elsif inverse.macro == :has_one
100
              record.send("#{inverse.name}=", build_through_record(record))
101
            end
102
          end
103 104

          record
105 106
        end

107
        def target_reflection_has_associated_record?
108
          if through_reflection.macro == :belongs_to && owner[through_reflection.foreign_key].blank?
109 110 111 112 113 114
            false
          else
            true
          end
        end

115 116 117
        def update_through_counter?(method)
          case method
          when :destroy
118
            !inverse_updates_counter_cache?(through_reflection)
119 120 121 122 123
          when :nullify
            false
          else
            true
          end
124
        end
125

126
        def delete_records(records, method)
127
          ensure_not_nested
J
Jon Leighton 已提交
128

J
Jon Leighton 已提交
129
          scope = through_association.scoped.where(construct_join_attributes(*records))
130

131 132
          case method
          when :destroy
133
            count = scope.destroy_all.length
134
          when :nullify
135
            count = scope.update_all(source_reflection.foreign_key => nil)
136
          else
137
            count = scope.delete_all
138 139
          end

J
Jon Leighton 已提交
140
          delete_through_records(records)
J
Jon Leighton 已提交
141

142 143
          if through_reflection.macro == :has_many && update_through_counter?(method)
            update_counter(-count, through_reflection)
144 145
          end

146
          update_counter(-count)
147
        end
148

149 150 151 152 153 154
        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 已提交
155
        def delete_through_records(records)
156
          records.each do |record|
157
            through_records = through_records_for(record)
158 159

            if through_reflection.macro == :has_many
J
Jon Leighton 已提交
160
              through_records.each { |r| through_association.target.delete(r) }
161
            else
J
Jon Leighton 已提交
162 163 164
              if through_records.include?(through_association.target)
                through_association.target = nil
              end
165
            end
166 167

            @through_records.delete(record.object_id)
168
          end
169 170
        end

171
        def find_target
172
          return [] unless target_reflection_has_associated_record?
173
          scoped.all
174
        end
175 176

        # NOTE - not sure that we can actually cope with inverses here
177
        def invertible_for?(record)
178 179
          false
        end
180 181 182
    end
  end
end