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

Add Source#result_key_for #4569

Merged
merged 4 commits into from
Jul 28, 2023
Merged

Add Source#result_key_for #4569

merged 4 commits into from
Jul 28, 2023

Conversation

rmosolgo
Copy link
Owner

This adds a way to deduplicate .load(...) calls when the passed-in object is equivalent but doesn't implement .hash to the same value.

@rmosolgo rmosolgo added this to the 2.0.25 milestone Jul 27, 2023
Copy link

@tgwizard tgwizard left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!


## De-duplicating equivalent objects

Sometimes, _different_ objects in the application should load the same object from `fetch`. You can customize this behavior by implementing `def result_key_for(key)` in your application. For example, to map records from your ORM to their database ID:

Choose a reason for hiding this comment

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

The use-case I have is actually the opposite - two Active Record objects that point to the same DB row both hash and eql? the same, but for my loader (a version of an AssociationLoader) needs to see all the records in fetch, without the deduping that happens when Active Record objects are used as keys in hashes/sets.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Got it -- I updated the doc to mention this case, as well. Thanks for clarifying!

@rmosolgo rmosolgo merged commit 30ea99d into master Jul 28, 2023
12 of 13 checks passed
@rmosolgo rmosolgo deleted the dataloader-source-key branch July 28, 2023 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants