Flowdock
method

execute_grouped_calculation

Importance_0
v2.3.2 - Show latest stable - 0 notes - Class: ActiveRecord::Calculations::ClassMethods
execute_grouped_calculation(operation, column_name, column, options) protected

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/calculations.rb, line 247
        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
Register or log in to add new notes.