aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_many_through_association.rb
blob: 664c284d452f92165d15ba086847da2b8a2c2b08 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require 'active_support/core_ext/object/blank'

module ActiveRecord
  # = Active Record Has Many Through Association
  module Associations
    class HasManyThroughAssociation < HasManyAssociation #:nodoc:
      include ThroughAssociation

      alias_method :new, :build

      # 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
      # SELECT query if you use #length.
      def size
        if has_cached_counter?
          @owner.send(:read_attribute, cached_counter_attribute_name)
        elsif loaded?
          @target.size
        else
          count
        end
      end

      def concat(*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

      def insert_record(record, validate = true)
        return if record.new_record? && !record.save(:validate => validate)
        through_record(record).save!
        update_counter(1)
        record
      end

      private

        def through_record(record)
          through_association = @owner.association(@reflection.through_reflection.name)
          attributes = construct_join_attributes(record)

          through_record = Array.wrap(through_association.target).find { |candidate|
            candidate.attributes.slice(*attributes.keys) == attributes
          }

          unless through_record
            through_record = through_association.build(attributes)
            through_record.send("#{@reflection.source_reflection.name}=", record)
          end

          through_record
        end

        def build_record(attributes)
          record = super(attributes)

          inverse = @reflection.source_reflection.inverse_of
          if inverse
            if inverse.macro == :has_many
              record.send(inverse.name) << through_record(record)
            elsif inverse.macro == :has_one
              record.send("#{inverse.name}=", through_record(record))
            end
          end

          record
        end

        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

        def update_through_counter?(method)
          case method
          when :destroy
            !inverse_updates_counter_cache?(@reflection.through_reflection)
          when :nullify
            false
          else
            true
          end
        end

        def delete_records(records, method)
          through = @owner.association(@reflection.through_reflection.name)
          scope   = through.scoped.where(construct_join_attributes(*records))

          case method
          when :destroy
            count = scope.destroy_all.length
          when :nullify
            count = scope.update_all(@reflection.source_reflection.foreign_key => nil)
          else
            count = scope.delete_all
          end

          delete_through_records(through, records)

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

          update_counter(-count)
        end

        def delete_through_records(through, records)
          if @reflection.through_reflection.macro == :has_many
            records.each do |record|
              through.target.delete(through_record(record))
            end
          else
            records.each do |record|
              through.target = nil if through.target == through_record(record)
            end
          end
        end

        def find_target
          return [] unless target_reflection_has_associated_record?
          scoped.all
        end

        # NOTE - not sure that we can actually cope with inverses here
        def invertible_for?(record)
          false
        end
    end
  end
end