Sha256: d9ecd13ac09b1d4c2cb0174ce32bb9c1cad173613d0ecdf82f84973883858008

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module SecondLevelCache
  module ActiveRecord
    module Associations
      module HasOneAssociation
        def find_target
          return super unless klass.second_level_cache_enabled?
          return super if klass.default_scopes.present? || reflection.scope
          # TODO: implement cache with has_one scope

          through = reflection.options[:through]
          record  = if through
            return super if klass.reflections[through.to_s].nil?
            return super unless klass.reflections[through.to_s].klass.second_level_cache_enabled?
            begin
              reflection.klass.find(owner.send(through).read_attribute(reflection.foreign_key))
            rescue StandardError
              nil
            end
          else
            uniq_keys = { reflection.foreign_key => owner[reflection.active_record_primary_key] }
            uniq_keys[reflection.type] = owner.class.base_class.name if reflection.options[:as]
            klass.fetch_by_uniq_keys(uniq_keys)
          end

          return nil unless record
          record.tap { |r| set_inverse_instance(r) }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
second_level_cache-2.6.4 lib/second_level_cache/active_record/has_one_association.rb