1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reuse AR::Association#find_target method

This commit is contained in:
Bogdan Gusiev 2018-12-27 11:53:27 +02:00
parent 4b5c4ca377
commit bc30d421f3
4 changed files with 21 additions and 29 deletions

View file

@ -179,6 +179,23 @@ module ActiveRecord
end
private
def find_target
scope = self.scope
return scope.to_a if skip_statement_cache?(scope)
conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do |params|
as = AssociationScope.create { params.bind }
target_scope.merge!(as.scope(self))
end
binds = AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute(binds, conn) do |record|
set_inverse_instance(record)
end
end
# The scope for this association.
#
# Note that the association_scope is merged into the target_scope only when the

View file

@ -26,7 +26,9 @@ module ActiveRecord
chain = get_chain(reflection, association, scope.alias_tracker)
scope.extending! reflection.extensions
add_constraints(scope, owner, chain)
scope = add_constraints(scope, owner, chain)
scope.limit!(1) unless reflection.collection?
scope
end
def self.get_bind_values(owner, chain)

View file

@ -303,21 +303,6 @@ module ActiveRecord
end
private
def find_target
scope = self.scope
return scope.to_a if skip_statement_cache?(scope)
conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do |params|
as = AssociationScope.create { params.bind }
target_scope.merge!(as.scope(self))
end
binds = AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute(binds, conn) do |record|
set_inverse_instance(record)
end
end
# We have some records loaded from the database (persisted) and some that are
# in-memory (memory). The same record may be represented in the persisted array

View file

@ -36,19 +36,7 @@ module ActiveRecord
end
def find_target
scope = self.scope
return scope.take if skip_statement_cache?(scope)
conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do |params|
as = AssociationScope.create { params.bind }
target_scope.merge!(as.scope(self)).limit(1)
end
binds = AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute(binds, conn) do |record|
set_inverse_instance record
end.first
super.first
rescue ::RangeError
nil
end