Class: ActiveRecord::Embedded::Aggregation::Native

Inherits:
ActiveRecord::Embedded::Aggregation show all
Defined in:
lib/active_record/embedded/aggregation/native.rb

Overview

Describe how to aggregate over embedded data across multiple parent models using Ruby's Enumerable methods. Because this causes multiple iterations over an object, it's best to use a database-specific adapter such as :postgresql if you need aggregations.

Direct Known Subclasses

Postgresql

Instance Method Summary collapse

Methods inherited from ActiveRecord::Embedded::Aggregation

create, #each, find, #initialize, #inspect

Methods included from Query

#build, #create, #create!, #find, #find!, #find_by, #find_by!, #initialize, #order, #where

Constructor Details

This class inherits a constructor from ActiveRecord::Embedded::Aggregation

Instance Method Details

#query_for(model) ⇒ Object


14
15
16
# File 'lib/active_record/embedded/aggregation/native.rb', line 14

def query_for(model)
  model.public_send(as).where(@filters)
end

#resultsObject


18
19
20
21
22
23
# File 'lib/active_record/embedded/aggregation/native.rb', line 18

def results
  @results ||= parent
               .all
               .map { |model| [model, query_for(model)] }
               .select { |_model, items| items.any? }
end