Module: ActiveRecord::Embedded::Model

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Model, Attributes, Fields, Indexing, Persistence, Querying, Storage, Integration, Comparable
Defined in:
lib/active_record/embedded/model.rb,
lib/active_record/embedded/model/fields.rb,
lib/active_record/embedded/model/storage.rb,
lib/active_record/embedded/model/indexing.rb,
lib/active_record/embedded/model/querying.rb,
lib/active_record/embedded/model/attributes.rb,
lib/active_record/embedded/model/persistence.rb

Overview

Mix this into your embedded model classes to provide ActiveRecord::Embedded functionality, including field/index definition, validations, callbacks, and AR-style fields like timestamps and identifiers.

Defined Under Namespace

Modules: Attributes, Fields, Indexing, Persistence, Querying, Storage

Instance Method Summary collapse

Methods included from Persistence

#destroy, #destroy!, #new_record?, #persisted?, #reload, #save, #save!, #update, #update!, #valid?

Methods included from Attributes

#[], #[]=, #assign_attributes, #inspect, #key?

Instance Method Details

#==(other) ⇒ Boolean

Another record is equal to this model if its #id is the same.

Returns:

  • (Boolean)

    whether both models’ IDs are equal



42
43
44
45
46
# File 'lib/active_record/embedded/model.rb', line 42

def ==(other)
  return false if id.blank?

  id == other&.id
end

#cache_key(*timestamp_names) ⇒ String

Prefix the embedded model’s cache key with the parent model’s cache key.

Returns:

  • (String)

    a stable cache key that can be used to identify this embedded record.



53
54
55
# File 'lib/active_record/embedded/model.rb', line 53

def cache_key(*timestamp_names)
  "#{_parent.cache_key}/#{super}"
end

#initialize(uncased = {}, _parent: nil, _association: nil, **params) ⇒ Object

Parameters:

  • _parent (ActiveRecord::Base) (defaults to: nil)
  • _association (Embedded::Association) (defaults to: nil)
    • Relationship metadata

  • attributes (Hash)
    • Additional model attributes



29
30
31
32
33
34
35
36
37
# File 'lib/active_record/embedded/model.rb', line 29

def initialize(uncased = {}, _parent: nil, _association: nil, **params)
  @_parent = _parent || params[parent_model.name]
  @_association = _association
  @attributes = amalgamate_attributes(uncased, params)

  run_callbacks :initialize do
    super(attributes)
  end
end