-
Notifications
You must be signed in to change notification settings - Fork 174
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 fetch_multi_by_* support for cache_index with a single field #368
Conversation
They need tests still |
@dylanahsmith are these changes ready? |
@dylanahsmith could you finish these changes please? |
94f076f
to
724d6bf
Compare
Updates: added test for happy paths. Some discussion needed about ideal return values (why is it an array, should it be a hash?). Maybe should add some failure scenario tests as well. |
|
||
assert_equal [@bob], Item.fetch_by_title('bob') | ||
|
||
assert_equal [@bob, @bertha], Item.fetch_multi_by_title(['bob', 'bertha']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it would be more consistent to return [[@bob], [@bertha]]
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fetch_multi_by_title
would always have a predictable return type. Wrapping each value in an array seems unnecessarily inconvenient from the perspective of the caller of that method. Consistency would only make sense if the caller was something generic that had to work with any of these indexes, since that seems like the only type of caller would have to special case single column return values.
Also, this behaviour is consistent with how active record's pluck
method works, which similarly asks for a fixed set of columns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh wait, this is supposed to be a non-unique index. I guess the problem is that the caller would have to associate the given values back to the given arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fetch_multi_by_#{key}
has the same return type whether the cache is unique: true
or not.
All other fetch methods, including fetch_multi_#{alias}_by_#{key}
, have different return types depending on the uniqueness of the cache.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use nested arrays for fetch_multi_by_#{key}
, then it seems like that would imply that it would always return an array of the same size as the given keys, such that it could be be used with zip
ed together with the input keys to get a hash of results (e.g. Hash[Item.fetch_multi_by_title(titles).zip(titles)]
).
However, I worry that might set the expectation that fetch_multi_by_#{key}
and fetch_multi
will work similarly. However, fetch_multi
returns a compacted array, which means it wouldn't work properly with zip
using the input keys array.
I think I chose to try to keep If a hash is desired, then |
I think in general, it's a bit weird that the caller can call the same type of method but get different results depending on some internal configuration: Product.fetch_multi_title_by_title(["hello"])
=> { "hello" => ["hello"] }
Product.fetch_multi_title_by_description(["world"])
=> { "world" => "hello" } How does the caller know if something is unique or not? |
@Matt888
Also, that seems similar to how |
product.variants => [variant, variant] # has_many uses plural name
product.image => image # has_one uses single name |
I'm still unclear on what you are proposing, since it seems like you are criticizing the existing API, which I am trying to make this feature consistent with. Breaking changes should be out of scope for a feature addition PR like this. |
724d6bf
to
5d2c5ef
Compare
c0e9992
to
45a135e
Compare
@dylanahsmith Are these change ready?