File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed
lib/active_record/relation Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -365,7 +365,12 @@ def exists?(conditions = :none)
365
365
end
366
366
367
367
return false if !conditions || limit_value == 0
368
- return records . any? ( &:persisted? ) if conditions == :none && loaded?
368
+
369
+ # Ignore if we have records which have saved changes since the load, because the
370
+ # relation can be a CollectionProxy and we updated the reference to the owner record.
371
+ if conditions == :none && loaded? && records . none? ( &:saved_changes? )
372
+ return records . any? ( &:persisted? )
373
+ end
369
374
370
375
if eager_loading?
371
376
relation = apply_join_dependency ( eager_loading : false )
Original file line number Diff line number Diff line change @@ -310,7 +310,23 @@ def test_exists_with_loaded_relation_having_unsaved_records
310
310
assert_not_empty posts
311
311
posts . each ( &:destroy )
312
312
313
- assert_not_predicate posts , :exists?
313
+ assert_no_queries do
314
+ assert_not_predicate posts , :exists?
315
+ end
316
+ end
317
+
318
+ def test_exists_with_loaded_relation_having_updated_owner_record
319
+ author = authors ( :david )
320
+ assert_not_empty author . posts
321
+
322
+ author . posts . each do |post |
323
+ post . author = nil
324
+ post . save!
325
+ end
326
+
327
+ assert_queries_count ( 1 ) do
328
+ assert_not_predicate author . posts , :exists?
329
+ end
314
330
end
315
331
316
332
# exists? should handle nil for id's that come from URLs and always return false
You can’t perform that action at this time.
0 commit comments