has_many_association.rb 5.0 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2 3 4
module ActiveRecord
  module Associations
    class HasManyAssociation < AssociationCollection #:nodoc:
      def initialize(owner, association_name, association_class_name, association_class_primary_key_name, options)
5
        super
6
        @conditions = sanitize_sql(options[:conditions])
D
Initial  
David Heinemeier Hansson 已提交
7

8
        construct_sql
D
Initial  
David Heinemeier Hansson 已提交
9 10 11
      end

      def build(attributes = {})
12 13 14 15 16 17 18 19 20
        if attributes.is_a?(Array)
          attributes.collect { |attr| create(attr) }
        else
          load_target
          record = @association_class.new(attributes)
          record[@association_class_primary_key_name] = @owner.id unless @owner.new_record?
          @target << record
          record
        end
D
Initial  
David Heinemeier Hansson 已提交
21 22
      end

23 24 25
      def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil)
        if @options[:finder_sql]
          records = @association_class.find_by_sql(@finder_sql)
D
Initial  
David Heinemeier Hansson 已提交
26
        else
27 28
          sql = @finder_sql
          sql += " AND #{sanitize_sql(runtime_conditions)}" if runtime_conditions
29 30
          orderings ||= @options[:order]
          records = @association_class.find_all(sql, orderings, limit, joins)
D
Initial  
David Heinemeier Hansson 已提交
31 32 33
        end
      end

34 35
      # Count the number of associated records. All arguments are optional.
      def count(runtime_conditions = nil)
36 37 38
        if @options[:counter_sql]
          @association_class.count_by_sql(@counter_sql)
        elsif @options[:finder_sql]
39 40 41
          @association_class.count_by_sql(@finder_sql)
        else
          sql = @finder_sql
42
          sql += " AND #{sanitize_sql(runtime_conditions)}" if runtime_conditions
43 44 45 46
          @association_class.count(sql)
        end
      end
      
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
      # Find the first associated record.  All arguments are optional.
      def find_first(conditions = nil, orderings = nil)
        find_all(conditions, orderings, 1).first
      end

      def find(*args)
        # Return an Array if multiple ids are given.
        expects_array = args.first.kind_of?(Array)

        ids = args.flatten.compact.uniq

        # If no ids given, raise RecordNotFound.
        if ids.empty?
          raise RecordNotFound, "Couldn't find #{@association_class.name} without an ID"

        # If using a custom finder_sql, scan the entire collection.
        elsif @options[:finder_sql]
          if ids.size == 1
            id = ids.first
66
            record = load_target.detect { |record| id == record.id }
67 68
            expects_array? ? [record] : record
          else
69
            load_target.select { |record| ids.include?(record.id) }
70 71 72
          end

        # Otherwise, delegate to association class with conditions.
D
Initial  
David Heinemeier Hansson 已提交
73
        else
74
          args << { :conditions => "#{@association_class_primary_key_name} = #{@owner.quoted_id} #{@conditions ? " AND " + @conditions : ""}" }
75
          @association_class.find(*args)
D
Initial  
David Heinemeier Hansson 已提交
76 77 78 79 80 81
        end
      end

      # Removes all records from this association.  Returns +self+ so
      # method calls may be chained.
      def clear
82
        @association_class.update_all("#{@association_class_primary_key_name} = NULL", "#{@association_class_primary_key_name} = #{@owner.quoted_id}")
83
        @target = []
D
Initial  
David Heinemeier Hansson 已提交
84 85 86 87
        self
      end

      protected
88
        def find_target
89
          find_all
D
Initial  
David Heinemeier Hansson 已提交
90
        end
91

D
Initial  
David Heinemeier Hansson 已提交
92 93 94
        def count_records
          if has_cached_counter?
            @owner.send(:read_attribute, cached_counter_attribute_name)
95
          elsif @options[:counter_sql]
D
Initial  
David Heinemeier Hansson 已提交
96 97 98 99 100
            @association_class.count_by_sql(@counter_sql)
          else
            @association_class.count(@counter_sql)
          end
        end
101

D
Initial  
David Heinemeier Hansson 已提交
102 103 104
        def has_cached_counter?
          @owner.attribute_present?(cached_counter_attribute_name)
        end
105

D
Initial  
David Heinemeier Hansson 已提交
106 107 108 109 110
        def cached_counter_attribute_name
          "#{@association_name}_count"
        end

        def insert_record(record)
111 112
          record[@association_class_primary_key_name] = @owner.id
          record.save
D
Initial  
David Heinemeier Hansson 已提交
113 114 115 116
        end

        def delete_records(records)
          ids = quoted_record_ids(records)
117 118 119 120
          @association_class.update_all(
            "#{@association_class_primary_key_name} = NULL", 
            "#{@association_class_primary_key_name} = #{@owner.quoted_id} AND #{@association_class.primary_key} IN (#{ids})"
          )
D
Initial  
David Heinemeier Hansson 已提交
121
        end
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

        def target_obsolete?
          false
        end

        def construct_sql
          if @options[:finder_sql]
            @finder_sql = interpolate_sql(@options[:finder_sql])
          else
            @finder_sql = "#{@association_class_primary_key_name} = #{@owner.quoted_id}"
            @finder_sql << " AND #{interpolate_sql(@conditions)}" if @conditions
          end

          if @options[:counter_sql]
            @counter_sql = interpolate_sql(@options[:counter_sql])
          elsif @options[:finder_sql]
            @options[:counter_sql] = @options[:finder_sql].gsub(/SELECT (.*) FROM/i, "SELECT COUNT(*) FROM")
            @counter_sql = interpolate_sql(@options[:counter_sql])
          else
            @counter_sql = "#{@association_class_primary_key_name} = #{@owner.quoted_id}"
            @counter_sql << " AND #{interpolate_sql(@conditions)}" if @conditions
          end
        end
D
Initial  
David Heinemeier Hansson 已提交
145 146 147
    end
  end
end