Module: ActiveRecord::Embedded::Query Abstract

Extended by:
ActiveSupport::Concern
Includes:
Enumerable
Included in:
Aggregation, Relation
Defined in:
lib/active_record/embedded/query.rb,
lib/active_record/embedded/query/no_solutions_error.rb

Overview

This module is abstract.

Include this module to provide querying functionality

Interface for querying embedded models. Implemented by the ActiveRecord::Embedded::Relation and ActiveRecord::Embedded::Aggregation objects, and provides the various SQL methods.

Defined Under Namespace

Classes: NoSolutionsError

Instance Method Summary collapse

Instance Method Details

#build(params = {}) ⇒ ActiveRecord::Embedded::Model

Instantiate a new model in this collection without persisting.

Parameters:

  • params (Hash) (defaults to: {})
    • Attributes to build the model with.

Returns:


42
43
44
# File 'lib/active_record/embedded/query.rb', line 42

def build(params = {})
  association.build(model, params)
end

#create(params = {}) ⇒ ActiveRecord::Embedded::Model

Create a new model in this collection.

Parameters:

  • params (Hash) (defaults to: {})
    • Attributes to build the model with.

Returns:


50
51
52
# File 'lib/active_record/embedded/query.rb', line 50

def create(params = {})
  association.build(model, params).tap(&:save)
end

#create!(params = {}) ⇒ ActiveRecord::Embedded::Model

Create a new model in this collection, and throw an exception if the operation fails.

Parameters:

  • params (Hash) (defaults to: {})
    • Attributes to build the model with.

Returns:


59
60
61
# File 'lib/active_record/embedded/query.rb', line 59

def create!(params = {})
  association.build(model, params).tap(&:save!)
end

#find(id) ⇒ ActiveRecord::Embedded::Model

Find a given model in the database by its ID.

Parameters:

  • ID (String)
    • Unique ID for the model you wish to find

Returns:


116
117
118
# File 'lib/active_record/embedded/query.rb', line 116

def find(id)
  find_by_index(id: id)
end

#find!(id) ⇒ ActiveRecord::Embedded::Model

Find a given model in the database by its ID. Throw an error when it cannot be found.

Parameters:

  • ID (String)
    • Unique ID for the model you wish to find

Returns:


125
126
127
# File 'lib/active_record/embedded/query.rb', line 125

def find!(id)
  find(id) || raise(RecordNotFound, id)
end

#find_by(params = {}) ⇒ ActiveRecord::Embedded::Model

Find a model by the given params. Uses an index if possible, otherwise performs a full table scan unless config.scan_tables is set to false.

Returns:

See Also:

  • for more information on what happens when indexes are used

97
98
99
# File 'lib/active_record/embedded/query.rb', line 97

def find_by(params = {})
  find_by_index(params) || where(params).first
end

#find_by!(params = {}) ⇒ ActiveRecord::Embedded::Model

Find a model by the given params. Uses an index if possible, otherwise performs a full table scan unless config.scan_tables is set to false. Throws an error when no record can be found by any means.

Returns:


108
109
110
# File 'lib/active_record/embedded/query.rb', line 108

def find_by!(params = {})
  find_by(params) || raise(RecordNotFound, params.to_sentence)
end

#initialize(model:, filters: {}, sorts: {}, association: nil, limit: -1,, start: 0) ⇒ Object

Parameters:

  • model (Model)
    • Subject of aggregation

  • filters (Hash)
    • Key/value pairs to match results on

  • sorts (Hash)
    • Key/value pairs to sort results with

  • association (Association)
    • Metadata for embedded relationship

  • limit (Integer)
    • Number of results to return

  • start (Integer)
    • Starting point in collection


27
28
29
30
31
32
33
34
35
36
# File 'lib/active_record/embedded/query.rb', line 27

def initialize(
  model:, filters: {}, sorts: {}, association: nil, limit: -1, start: 0
)
  @model = model
  @filters = filters
  @sorts = sorts
  @association = association
  @limit_value = limit
  @start_value = start
end

#order(sorts = {}) ⇒ ActiveRecord::Embedded::Relation

Order this collection by the given set of keys. Values are the direction, :desc or :asc.

Parameters:

  • sorts (Hash) (defaults to: {})
    • Key/direction pairs to sort by

Returns:


81
82
83
84
85
86
87
88
# File 'lib/active_record/embedded/query.rb', line 81

def order(sorts = {})
  self.class.new(
    association: association,
    model: model,
    filters: filters,
    sorts: sorts
  )
end

#where(filters = {}) ⇒ ActiveRecord::Embedded::Relation

Filter this collection by the given set of key/value pairs.

Parameters:

  • filters (Hash) (defaults to: {})
    • Key/value pairs to filter by

Returns:


67
68
69
70
71
72
73
74
# File 'lib/active_record/embedded/query.rb', line 67

def where(filters = {})
  self.class.new(
    association: association,
    model: model,
    filters: filters,
    sorts: sorts
  )
end