Skip to content

Commit f2ab8b6

Browse files
authored
Merge pull request rails#34538 from bogdan/reuse-find-target
Reuse code in AR::Association#find_target
2 parents b9c7305 + 68acbe8 commit f2ab8b6

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

activerecord/lib/active_record/associations/association.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ 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+
182199
# The scope for this association.
183200
#
184201
# Note that the association_scope is merged into the target_scope only when the

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,22 +304,6 @@ def find_from_target?
304304

305305
private
306306

307-
def find_target
308-
scope = self.scope
309-
return scope.to_a if skip_statement_cache?(scope)
310-
311-
conn = klass.connection
312-
sc = reflection.association_scope_cache(conn, owner) do |params|
313-
as = AssociationScope.create { params.bind }
314-
target_scope.merge!(as.scope(self))
315-
end
316-
317-
binds = AssociationScope.get_bind_values(owner, reflection.chain)
318-
sc.execute(binds, conn) do |record|
319-
set_inverse_instance(record)
320-
end
321-
end
322-
323307
# We have some records loaded from the database (persisted) and some that are
324308
# in-memory (memory). The same record may be represented in the persisted array
325309
# and in the memory array.

activerecord/lib/active_record/associations/singular_association.rb

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

3333
private
34+
35+
def target_scope
36+
super.limit!(1)
37+
end
38+
3439
def scope_for_create
3540
super.except!(klass.primary_key)
3641
end
3742

3843
def find_target
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
44+
super.first
5245
rescue ::RangeError
5346
nil
5447
end

0 commit comments

Comments
 (0)