Skip to content

Commit d2391f3

Browse files
authored
Merge pull request rails#46742 from ghiculescu/default-scope-reload
AR default scopes: only include `all_queries` default scopes on `reload`
2 parents 5972891 + f43544d commit d2391f3

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

activerecord/lib/active_record/persistence.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ def reload(options = nil)
10461046
self.class.connection.clear_query_cache
10471047

10481048
fresh_object = if apply_scoping?(options)
1049-
_find_record(options)
1049+
_find_record((options || {}).merge(all_queries: true))
10501050
else
10511051
self.class.unscoped { _find_record(options) }
10521052
end
@@ -1120,10 +1120,13 @@ def strict_loaded_associations
11201120
end
11211121

11221122
def _find_record(options)
1123+
all_queries = options ? options[:all_queries] : nil
1124+
base = self.class.all(all_queries: all_queries).preload(strict_loaded_associations)
1125+
11231126
if options && options[:lock]
1124-
self.class.preload(strict_loaded_associations).lock(options[:lock]).find_by!(_in_memory_query_constraints_hash)
1127+
base.lock(options[:lock]).find_by!(_in_memory_query_constraints_hash)
11251128
else
1126-
self.class.preload(strict_loaded_associations).find_by!(_in_memory_query_constraints_hash)
1129+
base.find_by!(_in_memory_query_constraints_hash)
11271130
end
11281131
end
11291132

activerecord/lib/active_record/scoping/default.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def build_default_scope(relation = relation(), all_queries: nil)
170170
# If all_queries is nil, only execute on select and insert queries.
171171
#
172172
# If all_queries is true, check if the default_scope object has
173-
# all_queries set, then execute on all queries; select, insert, update
174-
# and delete.
173+
# all_queries set, then execute on all queries; select, insert, update,
174+
# delete, and reload.
175175
def execute_scope?(all_queries, default_scope_obj)
176176
all_queries.nil? || all_queries && default_scope_obj.all_queries
177177
end

activerecord/lib/active_record/scoping/named.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module ClassMethods
1919
#
2020
# You can define a scope that applies to all finders using
2121
# {default_scope}[rdoc-ref:Scoping::Default::ClassMethods#default_scope].
22-
def all
22+
def all(all_queries: nil)
2323
scope = current_scope
2424

2525
if scope
@@ -29,7 +29,7 @@ def all
2929
relation.merge!(scope)
3030
end
3131
else
32-
default_scoped
32+
default_scoped(all_queries: all_queries)
3333
end
3434
end
3535

activerecord/test/cases/scoping/default_scoping_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ def test_default_scope_with_all_queries_runs_on_reload
225225
assert_match(/mentor_id/, reload_sql)
226226
end
227227

228+
def test_default_scope_with_all_queries_runs_on_reload_but_default_scope_without_all_queries_does_not
229+
Mentor.create!
230+
dev = DeveloperWithIncludedMentorDefaultScopeNotAllQueriesAndDefaultScopeFirmWithAllQueries.create!(name: "Eileen")
231+
reload_sql = capture_sql { dev.reload }.first
232+
233+
assert_no_match(/mentor_id/, reload_sql)
234+
assert_match(/firm_id/, reload_sql)
235+
end
236+
228237
def test_nilable_default_scope_with_all_queries_runs_on_reload
229238
dev = DeveloperWithDefaultNilableFirmScopeAllQueries.create!(name: "Nikita")
230239
reload_sql = capture_sql { dev.reload }.first

0 commit comments

Comments
 (0)