Method: IdentityCache::ConfigurationDSL::ClassMethods#cache_has_one

Defined in:
lib/identity_cache/configuration_dsl.rb

#cache_has_one(association, embed:) ⇒ Object

Will cache an association to the class including IdentityCache. IdentityCache will keep the association values in the same cache entry as the parent.

Example:

class Product
  cache_has_one :store, embed: true
  cache_has_one :vendor, embed: :id
end

Parameters

association Symbol with the name of the association being cached

Options

  • embed: If ‘true`, IdentityCache will embed the associated record in the cache entries for this model, as well as all the embedded associations for the associated record recursively. If `:id`, it will only embed the id for the associated record.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/identity_cache/configuration_dsl.rb', line 82

def cache_has_one(association, embed:)
  ensure_base_model
  check_association_for_caching(association)
  reflection = reflect_on_association(association)
  association_class = case embed
  when :id
    Cached::Reference::HasOne
  when true
    Cached::Recursive::HasOne
  else
    raise NotImplementedError
  end

  cached_has_ones[association] = association_class.new(
    association,
    reflection: reflection,
  ).tap(&:build)
end