mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #34538 from bogdan/reuse-find-target
Reuse code in AR::Association#find_target
This commit is contained in:
commit
f2ab8b64d4
3 changed files with 23 additions and 29 deletions
|
@ -179,6 +179,23 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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.
|
# The scope for this association.
|
||||||
#
|
#
|
||||||
# Note that the association_scope is merged into the target_scope only when the
|
# Note that the association_scope is merged into the target_scope only when the
|
||||||
|
|
|
@ -304,22 +304,6 @@ module ActiveRecord
|
||||||
|
|
||||||
private
|
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
|
# 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
|
# in-memory (memory). The same record may be represented in the persisted array
|
||||||
# and in the memory array.
|
# and in the memory array.
|
||||||
|
|
|
@ -31,24 +31,17 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def target_scope
|
||||||
|
super.limit!(1)
|
||||||
|
end
|
||||||
|
|
||||||
def scope_for_create
|
def scope_for_create
|
||||||
super.except!(klass.primary_key)
|
super.except!(klass.primary_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_target
|
def find_target
|
||||||
scope = self.scope
|
super.first
|
||||||
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
|
|
||||||
rescue ::RangeError
|
rescue ::RangeError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue