Skip to content

Commit 54cc42b

Browse files
committed
ActiveRecord::Persistence#reload respects query_constraints config
1 parent 232a057 commit 54cc42b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

activerecord/lib/active_record/persistence.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,17 @@ def strict_loaded_associations
10411041

10421042
def _find_record(options)
10431043
if options && options[:lock]
1044-
self.class.preload(strict_loaded_associations).lock(options[:lock]).find(id)
1044+
self.class.preload(strict_loaded_associations).lock(options[:lock]).find_by!(_in_memory_query_constraints_hash)
10451045
else
1046-
self.class.preload(strict_loaded_associations).find(id)
1046+
self.class.preload(strict_loaded_associations).find_by!(_in_memory_query_constraints_hash)
1047+
end
1048+
end
1049+
1050+
def _in_memory_query_constraints_hash
1051+
return { @primary_key => id } unless self.class.query_constraints_list
1052+
1053+
self.class.query_constraints_list.index_with do |column_name|
1054+
attribute(column_name)
10471055
end
10481056
end
10491057

activerecord/test/cases/persistence_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,13 @@ def test_save_uses_query_constraints_config
13571357
assert_match(/WHERE .*color/, sql)
13581358
end
13591359

1360+
def test_reload_uses_query_constraints_config
1361+
clothing_item = clothing_items(:green_t_shirt)
1362+
sql = capture_sql { clothing_item.reload }.first
1363+
assert_match(/WHERE .*clothing_type/, sql)
1364+
assert_match(/WHERE .*color/, sql)
1365+
end
1366+
13601367
def test_destroy_uses_query_constraints_config
13611368
clothing_item = clothing_items(:green_t_shirt)
13621369
sql = capture_sql { clothing_item.destroy }.first

0 commit comments

Comments
 (0)