has_many_through_association.rb 3.9 KB
Newer Older
1 2
require "active_record/associations/through_association_scope"

3 4
module ActiveRecord
  module Associations
5
    class HasManyThroughAssociation < HasManyAssociation #:nodoc:
6 7
      include ThroughAssociationScope

8
      alias_method :new, :build
9 10

      def create!(attrs = nil)
11 12
        ensure_owner_is_not_new

13
        transaction do
14
          self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association! } : @reflection.create_association!)
15
          object
16 17 18
        end
      end

19
      def create(attrs = nil)
20 21
        ensure_owner_is_not_new

22
        transaction do
23
          self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association)
24 25 26 27
          object
        end
      end

28 29 30 31 32 33 34
      def destroy(*records)
        transaction do
          delete_records(flatten_deeper(records))
          super
        end
      end

35
      # Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded and
36 37
      # 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.
38
      def size
39 40 41
        return @owner.send(:read_attribute, cached_counter_attribute_name) if has_cached_counter?
        return @target.size if loaded?
        return count
42
      end
43
      
44
      protected
45 46 47 48 49 50 51 52
        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

53 54 55 56 57 58 59
        def construct_find_options!(options)
          options[:select]  = construct_select(options[:select])
          options[:from]  ||= construct_from
          options[:joins]   = construct_joins(options[:joins])
          options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil?
        end
        
60
        def insert_record(record, force = true, validate = true)
61 62 63 64
          if record.new_record?
            if force
              record.save!
            else
65
              return false unless record.save(validate)
66 67
            end
          end
68 69 70
          through_reflection = @reflection.through_reflection
          klass = through_reflection.klass
          @owner.send(@reflection.through_reflection.name).proxy_target << klass.send(:with_scope, :create => construct_join_attributes(record)) { through_reflection.create_association! }
71 72 73 74 75 76 77 78 79 80
        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

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

86 87 88 89 90
        def construct_sql
          case
            when @reflection.options[:finder_sql]
              @finder_sql = interpolate_sql(@reflection.options[:finder_sql])

91
              @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}"
92
              @finder_sql << " AND (#{conditions})" if conditions
93 94
            else
              @finder_sql = construct_conditions
95 96
          end

97
          construct_counter_sql
98
        end
99

100 101 102 103 104 105 106
        def has_cached_counter?
          @owner.attribute_present?(cached_counter_attribute_name)
        end

        def cached_counter_attribute_name
          "#{@reflection.name}_count"
        end
107 108 109 110 111

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