Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fetch has_many embedded association after adding to it #449

Merged
merged 4 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: identity-cache
up:
- homebrew:
- postgresql
- ruby: 2.4.1
- ruby: 2.4.10
- railgun
- bundler

Expand Down
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.blank?
#{records_variable_name} ||= #{reflection.class_name}.fetch_multi(#{cached_ids_name})
else
#{name}.to_a
Expand Down
4 changes: 2 additions & 2 deletions lib/identity_cache/cached/reference/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
7 changes: 7 additions & 0 deletions test/normalized_has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,11 @@ def test_respects_should_use_cache_on_association
end
end
end

def test_fetch_association_after_adding_to_it
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems to fail for Item.cache_has_many(:associated_records, embed: true), is that expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

I was thinking it was only an issue with id embedding, but I think that is just because the symptoms of the problem were different for id embedding.

I've pushed a commit fixing this for cache_has_many embed: true

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