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

Missing lazy context for intermediate association (has_one ... through) #74

Open
id-ilych opened this issue May 22, 2024 · 0 comments
Open

Comments

@id-ilych
Copy link

id-ilych commented May 22, 2024

Hello Dmitry 👋

I noticed an unexpected behavior in a context of has_one ... through associations.
Imagine a model:

class Alice < ApplicationRecord
  has_one :bob
  has_one :charlie, through: :bob
end

and then

record = Alice.preload_associations_lazily.find(some_id)
assert record.lazy_preload_context.present?, "lazy context is missing for alice"

# at this point record has no associations loaded
record.charlie
# at this point record has both bob and charlie associations loaded

assert record.charlie.lazy_preload_context.present?, "lazy context is missing for charlie"
assert record.bob.lazy_preload_context.present?, "lazy context is missing for bob"

Observed behavior: the last assertion fails
Expected behavior: all assertions succeed

Steps to reproduce:

  1. Clone https://github.com/id-ilych/ar_lazy_preload_bug
  2. docker compose up -d && bin/setup && bin/rails test

Output:

% bin/rails test
Running 1 tests in a single process (parallelization threshold is 50)
Run options: --seed 56792

# Running:

F

Failure:
AliceTest#test_the_truth [test/models/alice_test.rb:15]:
lazy context is missing for bob


rails test test/models/alice_test.rb:4



Finished in 0.127767s, 7.8267 runs/s, 46.9605 assertions/s.
1 runs, 6 assertions, 1 failures, 0 errors, 0 skips
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

No branches or pull requests

1 participant