Skip to content

Commit

Permalink
Merge pull request #46402 from Shopify/reload-method-should-respect-q…
Browse files Browse the repository at this point in the history
…uery-constraints

`ActiveRecord::Persistence#reload` respects `query_constraints` config
  • Loading branch information
eileencodes committed Nov 3, 2022
2 parents 07f48f7 + 54cc42b commit 8069d7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 10 additions & 2 deletions activerecord/lib/active_record/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,17 @@ def strict_loaded_associations

def _find_record(options)
if options && options[:lock]
self.class.preload(strict_loaded_associations).lock(options[:lock]).find(id)
self.class.preload(strict_loaded_associations).lock(options[:lock]).find_by!(_in_memory_query_constraints_hash)
else
self.class.preload(strict_loaded_associations).find(id)
self.class.preload(strict_loaded_associations).find_by!(_in_memory_query_constraints_hash)
end
end

def _in_memory_query_constraints_hash
return { @primary_key => id } unless self.class.query_constraints_list

self.class.query_constraints_list.index_with do |column_name|
attribute(column_name)
end
end

Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/persistence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,13 @@ def test_save_uses_query_constraints_config
assert_match(/WHERE .*color/, sql)
end

def test_reload_uses_query_constraints_config
clothing_item = clothing_items(:green_t_shirt)
sql = capture_sql { clothing_item.reload }.first
assert_match(/WHERE .*clothing_type/, sql)
assert_match(/WHERE .*color/, sql)
end

def test_destroy_uses_query_constraints_config
clothing_item = clothing_items(:green_t_shirt)
sql = capture_sql { clothing_item.destroy }.first
Expand Down

0 comments on commit 8069d7f

Please sign in to comment.