Class: ActiveRecord::Calculations::ColumnAliasTracker

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/relation/calculations.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ ColumnAliasTracker

Returns a new instance of ColumnAliasTracker.



8
9
10
11
# File 'activerecord/lib/active_record/relation/calculations.rb', line 8

def initialize(connection)
  @connection = connection
  @aliases = Hash.new(0)
end

Instance Method Details

#alias_for(field) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'activerecord/lib/active_record/relation/calculations.rb', line 13

def alias_for(field)
  aliased_name = column_alias_for(field)

  if @aliases[aliased_name] == 0
    @aliases[aliased_name] = 1
    aliased_name
  else
    # Update the count
    count = @aliases[aliased_name] += 1
    "#{truncate(aliased_name)}_#{count}"
  end
end