Module: SecondLevelCache::ActiveRecord::Associations::HasOneAssociation

Defined in:
lib/second_level_cache/active_record/has_one_association.rb

Instance Method Summary collapse

Instance Method Details

#find_targetObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/second_level_cache/active_record/has_one_association.rb', line 7

def find_target
  return super unless klass.second_level_cache_enabled?
  return super if reflection.options[:through] || reflection.scope
  # TODO: implement cache with has_one through, scope

  owner_primary_key = owner[reflection.active_record_primary_key]
  if reflection.options[:as]
    keys = {
      reflection.foreign_key => owner_primary_key,
      reflection.type => owner.class.base_class.name
    }
    cache_record = klass.fetch_by_uniq_keys(keys)
  else
    cache_record = klass.fetch_by_uniq_key(owner_primary_key, reflection.foreign_key)
  end

  if cache_record
    return cache_record.tap { |record| set_inverse_instance(record) }
  end

  record = super
  return nil unless record

  record.tap do |r|
    set_inverse_instance(r)
    r.write_second_level_cache
  end
end