diff --git a/dev.yml b/dev.yml index ce7e70e5..c41a870f 100644 --- a/dev.yml +++ b/dev.yml @@ -3,7 +3,7 @@ name: identity-cache up: - homebrew: - postgresql - - ruby: 2.4.1 + - ruby: 2.4.10 - railgun - bundler diff --git a/lib/identity_cache/cached/reference/has_many.rb b/lib/identity_cache/cached/reference/has_many.rb index 9870b206..1d868069 100644 --- a/lib/identity_cache/cached/reference/has_many.rb +++ b/lib/identity_cache/cached/reference/has_many.rb @@ -20,8 +20,8 @@ def #{cached_ids_name} end def #{cached_accessor_name} - association_klass = association(:#{name}).klass - if association_klass.should_use_cache? && !#{name}.loaded? + assoc = association(:#{name}) + if assoc.klass.should_use_cache? && !assoc.loaded? && assoc.target.blank? #{records_variable_name} ||= #{reflection.class_name}.fetch_multi(#{cached_ids_name}) else #{name}.to_a diff --git a/lib/identity_cache/cached/reference/has_one.rb b/lib/identity_cache/cached/reference/has_one.rb index 6db82e79..160549f4 100644 --- a/lib/identity_cache/cached/reference/has_one.rb +++ b/lib/identity_cache/cached/reference/has_one.rb @@ -21,8 +21,8 @@ def #{cached_id_name} end def #{cached_accessor_name} - association_klass = association(:#{name}).klass - if association_klass.should_use_cache? && !association(:#{name}).loaded? + assoc = association(:#{name}) + if assoc.klass.should_use_cache? && !assoc.loaded? #{records_variable_name} ||= #{reflection.class_name}.fetch(#{cached_id_name}) if #{cached_id_name} else #{name} diff --git a/lib/identity_cache/query_api.rb b/lib/identity_cache/query_api.rb index 47d10ecc..70005c15 100644 --- a/lib/identity_cache/query_api.rb +++ b/lib/identity_cache/query_api.rb @@ -168,7 +168,7 @@ def was_new_record? # :nodoc: def fetch_recursively_cached_association(ivar_name, dehydrated_ivar_name, association_name) # :nodoc: assoc = association(association_name) - if assoc.klass.should_use_cache? && !assoc.loaded? + if assoc.klass.should_use_cache? && !assoc.loaded? && assoc.target.blank? if instance_variable_defined?(ivar_name) instance_variable_get(ivar_name) elsif instance_variable_defined?(dehydrated_ivar_name) diff --git a/test/denormalized_has_many_test.rb b/test/denormalized_has_many_test.rb index 355423f7..773f90a7 100644 --- a/test/denormalized_has_many_test.rb +++ b/test/denormalized_has_many_test.rb @@ -180,6 +180,13 @@ def test_respect_should_use_cache_from_embedded_records end end + def test_fetch_association_after_adding_to_it + item = Item.fetch(@record.id) + item.associated_records.create!(name: 'foo') + fetched_associated_records = item.fetch_associated_records + assert_equal(item.associated_records.length, fetched_associated_records.length) + end + class CheckAssociationTest < IdentityCache::TestCase def test_unsupported_through_assocation assert_raises IdentityCache::UnsupportedAssociationError, "caching through associations isn't supported" do diff --git a/test/normalized_has_many_test.rb b/test/normalized_has_many_test.rb index c15c679e..1679ca54 100644 --- a/test/normalized_has_many_test.rb +++ b/test/normalized_has_many_test.rb @@ -244,4 +244,11 @@ def test_respects_should_use_cache_on_association end end end + + def test_fetch_association_after_adding_to_it + item = Item.fetch(@record.id) + item.associated_records.create!(name: 'foo') + fetched_associated_records = item.fetch_associated_records + assert_equal(item.associated_records.length, fetched_associated_records.length) + end end diff --git a/test/prefetch_associations_test.rb b/test/prefetch_associations_test.rb index 1f4acfb2..031908e4 100644 --- a/test/prefetch_associations_test.rb +++ b/test/prefetch_associations_test.rb @@ -134,7 +134,7 @@ def test_prefetch_associations_on_association setup_has_many_children_and_grandchildren(@bob) - associated_records = @bob.associated_records + associated_records = Item.find(@bob.id).associated_records assert_queries(1) do assert_memcache_operations(1) do