has_many_through_association.rb 3.2 KB
Newer Older
1
require "active_record/associations/through_association_scope"
2
require 'active_support/core_ext/object/blank'
3

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

10
      alias_method :new, :build
11 12

      def create!(attrs = nil)
13
        create_record(attrs, true)
14 15
      end

16
      def create(attrs = nil)
17
        create_record(attrs, false)
18 19
      end

20 21 22 23 24 25 26
      def destroy(*records)
        transaction do
          delete_records(flatten_deeper(records))
          super
        end
      end

27 28 29
      # 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
30
      # SELECT query if you use #length.
31
      def size
32 33 34
        return @owner.send(:read_attribute, cached_counter_attribute_name) if has_cached_counter?
        return @target.size if loaded?
        return count
35
      end
36

37
      protected
38
        def create_record(attrs, force = true)
39
          ensure_owner_is_persisted!
40 41

          transaction do
42
            object = @reflection.klass.new(attrs)
43 44 45 46 47
            add_record_to_target_with_callbacks(object) {|r| insert_record(object, force) }
            object
          end
        end

48 49 50 51 52 53 54 55
        def target_reflection_has_associated_record?
          if @reflection.through_reflection.macro == :belongs_to && @owner[@reflection.through_reflection.primary_key_name].blank?
            false
          else
            true
          end
        end

56
        def construct_find_options!(options)
A
Aaron Patterson 已提交
57
          options[:joins]   = [construct_joins] + Array.wrap(options[:joins])
58
          options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil? && @reflection.source_reflection.options[:include]
59
        end
60

61
        def insert_record(record, force = true, validate = true)
62
          if record.new_record?
63 64 65
            if force
              record.save!
            else
66
              return false unless record.save(:validate => validate)
67 68
            end
          end
69 70

          through_association = @owner.send(@reflection.through_reflection.name)
S
Santiago Pastorino 已提交
71
          through_association.create!(construct_join_attributes(record))
72 73 74 75 76 77 78 79 80 81
        end

        # TODO - add dependent option support
        def delete_records(records)
          klass = @reflection.through_reflection.klass
          records.each do |associate|
            klass.delete_all(construct_join_attributes(associate))
          end
        end

82
        def find_target
83
          return [] unless target_reflection_has_associated_record?
84
          with_scope(@scope) { @reflection.klass.find(:all) }
85
        end
86

87 88 89 90 91 92 93
        def has_cached_counter?
          @owner.attribute_present?(cached_counter_attribute_name)
        end

        def cached_counter_attribute_name
          "#{@reflection.name}_count"
        end
94 95 96 97 98

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