Skip to content

Commit

Permalink
Fix fetch has_many id embedded association after adding to it
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith committed Apr 3, 2020
1 parent aca0185 commit 0791ba7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/identity_cache/cached/reference/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.empty?
#{records_variable_name} ||= #{reflection.class_name}.fetch_multi(#{cached_ids_name})
else
#{name}.to_a
Expand Down
6 changes: 6 additions & 0 deletions test/normalized_has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,10 @@ 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')
assert_equal(item.associated_records.length, item.fetch_associated_records.length)
end
end

0 comments on commit 0791ba7

Please sign in to comment.