Module: ChronoModel::Patches::Association

Defined in:
lib/chrono_model/patches/association.rb

Overview

Patches ActiveRecord::Associations::Association to add support for temporal associations.

Each record fetched from the as_of scope on the owner class will have an additional “as_of_time” field yielding the UTC time of the request, then the as_of scope is called on either this association’s class or on the join model’s (:through association) one.

Instance Method Summary collapse

Instance Method Details

#scopeObject

If the association class or the through association are ChronoModels, then fetches the records from a virtual table using a subquery scope to a specific timestamp.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chrono_model/patches/association.rb', line 21

def scope
  scope = super
  return scope unless _chrono_record?

  if _chrono_target?
    # For standard associations, replace the table name with the virtual
    # as-of table name at the owner's as-of-time
    #
    scope = scope.from(klass.history.virtual_table_at(owner.as_of_time))
  end

  scope.as_of_time!(owner.as_of_time)

  scope
end

#skip_statement_cache?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/chrono_model/patches/association.rb', line 14

def skip_statement_cache?(*)
  super || _chrono_target?
end