mongoid any? returns true when collection has only destroyed objects #5445
Replies: 1 comment 1 reply
-
@jeffsaremi I think this comes down to how mongoid implements the I went in and redefined the
First off, if I call Ok, so given the above, we see that So this is just a weird bug where having the records cached in memory causes mongoid to return stale results since it doesn't know that the record was deleted |
Beta Was this translation helpful? Give feedback.
-
I have written this simple test to show that
any?
andcount
behave differently if applied to an attribute of typehas_many
.Running Rails 5.2.8 and mongoid 7.0.13
I have updated mongoid all the way to 8.0.2 with no change in behavior
https://github.com/jeffsaremi/mongoid-rspec/blob/master/spec/destroy_spec.rb
In the following example:
Line 43 should return false but it doesn't
However, if I avoid iterating through the
a_piece.parts
-- as in lines 30-32 -- then the results are as expected.Another change that would make the results correct is to enable line 38 meaning to
reload
the parent objectIf you look at the code for destroy it calls run_callbacks which calls cascadable_children
There is nowhere an attempt to go and fix up the parent classes (has_one, has_many) members! So unless records are persisted and reloaded afterwards the memory snapshot will always be inconsistent
Beta Was this translation helpful? Give feedback.
All reactions