Module: ActiveRecord::Embedded::Model

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Model, Attributes, Fields, Indexing, Persistence, Querying, Storage
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


36
37
38
39
40
# File 'lib/active_record/embedded/model.rb', line 36

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

  id == other&.id
end

#initialize(_parent: nil, _association: nil, **attributes) ⇒ Object

Parameters:

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

  • attributes (Hash)
    • Additional model attributes


23
24
25
26
27
28
29
30
31
# File 'lib/active_record/embedded/model.rb', line 23

def initialize(_parent: nil, _association: nil, **attributes)
  @_parent = _parent || attributes[parent_model.name]
  @_association = _association
  @attributes = attributes

  run_callbacks :initialize do
    super(attributes)
  end
end