method
execute_grouped_calculation
v2.1.0 -
Show latest stable
-
0 notes -
Class: ActiveRecord::Calculations::ClassMethods
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
execute_grouped_calculation(operation, column_name, column, options)
protected
Hide source
# File activerecord/lib/active_record/calculations.rb, line 217 def execute_grouped_calculation(operation, column_name, column, options) #:nodoc: group_attr = options[:group].to_s association = reflect_on_association(group_attr.to_sym) associated = association && association.macro == :belongs_to # only count belongs_to associations group_field = associated ? association.primary_key_name : group_attr group_alias = column_alias_for(group_field) group_column = column_for group_field sql = construct_calculation_sql(operation, column_name, options.merge(:group_field => group_field, :group_alias => group_alias)) calculated_data = connection.select_all(sql) aggregate_alias = column_alias_for(operation, column_name) if association key_ids = calculated_data.collect { |row| row[group_alias] } key_records = association.klass.base_class.find(key_ids) key_records = key_records.inject({}) { |hsh, r| hsh.merge(r.id => r) } end calculated_data.inject(ActiveSupport::OrderedHash.new) do |all, row| key = type_cast_calculated_value(row[group_alias], group_column) key = key_records[key] if associated value = row[aggregate_alias] all[key] = type_cast_calculated_value(value, column, operation) all end end