-
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
Fix fetch has_many embedded association after adding to it #449
Conversation
3983507
to
514b650
Compare
Also, avoids the redundant association lookup
@@ -244,4 +244,11 @@ def test_respects_should_use_cache_on_association | |||
end | |||
end | |||
end | |||
|
|||
def test_fetch_association_after_adding_to_it |
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.
This test seems to fail for Item.cache_has_many(:associated_records, embed: true)
, is that expected?
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.
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
@@ -134,7 +134,7 @@ def test_prefetch_associations_on_association | |||
|
|||
setup_has_many_children_and_grandchildren(@bob) | |||
|
|||
associated_records = @bob.associated_records |
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.
This test was failing because setup_has_many_children_and_grandchildren
was creating records on an association without loading it, which messes with preloading checks. So calling prefetch_associations
on records with a modified unloaded association like this may not be ideal, although I think a bigger refactor is needed to handle that complexity well. For instance, our check to see if an association has already been loaded/fetched assumes that it can just check the first record, but I don't think we want to make a similar assumption for associations being modified which are less likely to be homogeneous. I would also like to avoid adding too much overhead for the includes
fetch option, which can make more assumptions of the state of the records since they aren't coming from application code.
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.
Opened #453 to keep track of this issue.
cc @mgingras
Problem
The problem is shown well from the added regression test on a
cache_has_many
embed: :ids
associationwhich results in the failure
without the included fix to the code under test.
Solution
I looked at how Active Record distinguishes between loaded associations and unloaded associations with records added to it and found it checks for this with
elsif !target.empty?
. I decided to use a similar check, although usedblank?
so that we don't assume thattarget
will continue to be initialized to[]
for an unloaded association.