Module: TinyCache::ActiveRecord::Associations::HasOneAssociation

Extended by:
ActiveSupport::Concern
Defined in:
lib/tiny_cache/active_record/has_one_association.rb

Instance Method Summary collapse

Instance Method Details

#association_classObject



13
14
15
# File 'lib/tiny_cache/active_record/has_one_association.rb', line 13

def association_class
  @reflection.klass
end

#find_target_with_tiny_cacheObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tiny_cache/active_record/has_one_association.rb', line 17

def find_target_with_tiny_cache
  return find_target_without_tiny_cache unless association_class.tiny_cache_enabled?

  cache_record = association_class.tiny_cache_find_by(
    reflection.foreign_key => owner[reflection.active_record_primary_key]
  )

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

  record = find_target_without_tiny_cache

  record.tap do |r|
    set_inverse_instance(r)
    r.write_tiny_cache
  end if record
end