ar_lazy_preload
ar_lazy_preload copied to clipboard
Missing lazy context for intermediate association (has_one ... through)
Hello Dmitry :wave:
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:
- Clone https://github.com/id-ilych/ar_lazy_preload_bug
-
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