Method: ActiveRecord::Associations::AliasTracker.initial_count_for

Defined in:
activerecord/lib/active_record/associations/alias_tracker.rb

.initial_count_for(connection, name, table_joins) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'activerecord/lib/active_record/associations/alias_tracker.rb', line 28

def self.initial_count_for(connection, name, table_joins)
  quoted_name = nil

  counts = table_joins.map do |join|
    if join.is_a?(Arel::Nodes::StringJoin)
      # quoted_name should be case ignored as some database adapters (Oracle) return quoted name in uppercase
      quoted_name ||= connection.quote_table_name(name)

      # Table names + table aliases
      join.left.scan(
        /JOIN(?:\s+\w+)?\s+(?:\S+\s+)?(?:#{quoted_name}|#{name})\sON/i
      ).size
    elsif join.is_a?(Arel::Nodes::Join)
      join.left.name == name ? 1 : 0
    else
      raise ArgumentError, "joins list should be initialized by list of Arel::Nodes::Join"
    end
  end

  counts.sum
end