Skip to content

Commit 93c9497

Browse files
committed
Revert "Merge pull request rails#34538 from bogdan/reuse-find-target"
This reverts commit f2ab8b6, reversing changes made to b9c7305. Reason: `scope.take` is not the same with `scope.to_a.first`.
1 parent f2ab8b6 commit 93c9497

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

activerecord/lib/active_record/associations/association.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,6 @@ def create!(attributes = {}, &block)
179179
end
180180

181181
private
182-
183-
def find_target
184-
scope = self.scope
185-
return scope.to_a if skip_statement_cache?(scope)
186-
187-
conn = klass.connection
188-
sc = reflection.association_scope_cache(conn, owner) do |params|
189-
as = AssociationScope.create { params.bind }
190-
target_scope.merge!(as.scope(self))
191-
end
192-
193-
binds = AssociationScope.get_bind_values(owner, reflection.chain)
194-
sc.execute(binds, conn) do |record|
195-
set_inverse_instance(record)
196-
end
197-
end
198-
199182
# The scope for this association.
200183
#
201184
# Note that the association_scope is merged into the target_scope only when the

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,21 @@ def find_from_target?
303303
end
304304

305305
private
306+
def find_target
307+
scope = self.scope
308+
return scope.to_a if skip_statement_cache?(scope)
309+
310+
conn = klass.connection
311+
sc = reflection.association_scope_cache(conn, owner) do |params|
312+
as = AssociationScope.create { params.bind }
313+
target_scope.merge!(as.scope(self))
314+
end
315+
316+
binds = AssociationScope.get_bind_values(owner, reflection.chain)
317+
sc.execute(binds, conn) do |record|
318+
set_inverse_instance(record)
319+
end
320+
end
306321

307322
# We have some records loaded from the database (persisted) and some that are
308323
# in-memory (memory). The same record may be represented in the persisted array

activerecord/lib/active_record/associations/singular_association.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,24 @@ def force_reload_reader
3131
end
3232

3333
private
34-
35-
def target_scope
36-
super.limit!(1)
37-
end
38-
3934
def scope_for_create
4035
super.except!(klass.primary_key)
4136
end
4237

4338
def find_target
44-
super.first
39+
scope = self.scope
40+
return scope.take if skip_statement_cache?(scope)
41+
42+
conn = klass.connection
43+
sc = reflection.association_scope_cache(conn, owner) do |params|
44+
as = AssociationScope.create { params.bind }
45+
target_scope.merge!(as.scope(self)).limit(1)
46+
end
47+
48+
binds = AssociationScope.get_bind_values(owner, reflection.chain)
49+
sc.execute(binds, conn) do |record|
50+
set_inverse_instance record
51+
end.first
4552
rescue ::RangeError
4653
nil
4754
end

0 commit comments

Comments
 (0)