has_many_through_association.rb 3.8 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
        transaction do
12
          self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association! } : @reflection.create_association!)
13
          object
14 15 16
        end
      end

17
      def create(attrs = nil)
18
        transaction do
19
          self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.create_association } : @reflection.create_association)
20 21 22 23
          object
        end
      end

24 25 26 27 28 29 30
      def destroy(*records)
        transaction do
          delete_records(flatten_deeper(records))
          super
        end
      end

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

49 50 51 52 53 54 55
        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
        
56
        def insert_record(record, force = true, validate = true)
57 58 59 60
          if record.new_record?
            if force
              record.save!
            else
61
              return false unless record.save(validate)
62 63
            end
          end
64 65 66
          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! }
67 68 69 70 71 72 73 74 75 76
        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

77
        def find_target
78
          return [] unless target_reflection_has_associated_record?
79
          with_scope(construct_scope) { @reflection.klass.find(:all) }
80
        end
81

82 83 84 85 86
        def construct_sql
          case
            when @reflection.options[:finder_sql]
              @finder_sql = interpolate_sql(@reflection.options[:finder_sql])

87
              @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}"
88
              @finder_sql << " AND (#{conditions})" if conditions
89 90
            else
              @finder_sql = construct_conditions
91 92
          end

93
          construct_counter_sql
94
        end
95

96 97 98 99 100 101 102
        def has_cached_counter?
          @owner.attribute_present?(cached_counter_attribute_name)
        end

        def cached_counter_attribute_name
          "#{@reflection.name}_count"
        end
103 104 105 106 107

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