Skip to content

Commit b7ec057

Browse files
committed
Using cache when within a transaction if already loaded
1 parent 2b12889 commit b7ec057

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/identity_cache/belongs_to_caching.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ def build_normalized_belongs_to_cache(association, options)
3434
foreign_key = options[:association_reflection].foreign_key
3535
self.class_eval(<<-CODE, __FILE__, __LINE__ + 1)
3636
def #{options[:cached_accessor_name]}
37-
if IdentityCache.should_use_cache? && #{foreign_key}.present? && !association(:#{association}).loaded?
38-
if instance_variable_defined?(:@#{options[:records_variable_name]})
39-
@#{options[:records_variable_name]}
40-
else
41-
@#{options[:records_variable_name]} = association(:#{association}).klass.fetch_by_id(#{foreign_key})
42-
end
37+
if association(:#{association}).loaded? || #{foreign_key}.blank?
38+
#{association}
39+
elsif instance_variable_defined?(:@#{options[:records_variable_name]})
40+
@#{options[:records_variable_name]}
41+
elsif IdentityCache.should_use_cache?
42+
@#{options[:records_variable_name]} = association(:#{association}).klass.fetch_by_id(#{foreign_key})
4343
else
4444
#{association}
4545
end

0 commit comments

Comments
 (0)