class method self.expand_hash_conditions_for_aggregates

Ruby on Rails 2.3.18

Since v2.2.3 Last seen in v3.1.12

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12

Signature

self.expand_hash_conditions_for_aggregates(attrs)

Accepts a hash of SQL conditions and replaces those attributes that correspond to a composed_of relationship with their expanded aggregate attribute values. Given:

class Person < ActiveRecord::Base
  composed_of :address, :class_name => "Address",
    :mapping => [%w(address_street street), %w(address_city city)]
end

Then:

{ :address => Address.new("813 abc st.", "chicago") }
  # => { :address_street => "813 abc st.", :address_city => "chicago" }

Parameters

attrs req
Source
# File activerecord/lib/active_record/base.rb, line 2307
        def expand_hash_conditions_for_aggregates(attrs)
          expanded_attrs = {}
          attrs.each do |attr, value|
            unless (aggregation = reflect_on_aggregation(attr)).nil?
              mapping = aggregate_mapping(aggregation)
              mapping.each do |field_attr, aggregate_attr|
                if mapping.size == 1 && !value.respond_to?(aggregate_attr)
                  expanded_attrs[field_attr] = value
                else
                  expanded_attrs[field_attr] = value.send(aggregate_attr)
                end
              end
            else
              expanded_attrs[attr] = value
            end
          end
          expanded_attrs
        end

Defined in activerecord/lib/active_record/base.rb line 2307 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Base

Type at least 2 characters to search.

↑↓ navigate · open · esc close