提交 954b1970 编写于 作者: R Ryuta Kamizono

Avoid duplicated `set_inverse_instance` for target scope

Because `scope` (`target_scope`) is a `AssociationRelation`.
`AssociationRelation` handles `set_inverse_instance`.

https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/association_relation.rb#L31-L33

See also #26022.
上级 d46d61e4
...@@ -455,23 +455,20 @@ def null_scope? ...@@ -455,23 +455,20 @@ def null_scope?
end end
private private
def get_records
return scope.to_a if skip_statement_cache?
conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do
StatementCache.create(conn) { |params|
as = AssociationScope.create { params.bind }
target_scope.merge as.scope(self, conn)
}
end
binds = AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute binds, klass, klass.connection
end
def find_target def find_target
records = get_records return scope.to_a if skip_statement_cache?
conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do
StatementCache.create(conn) { |params|
as = AssociationScope.create { params.bind }
target_scope.merge as.scope(self, conn)
}
end
binds = AssociationScope.get_bind_values(owner, reflection.chain)
records = sc.execute(binds, klass, conn)
records.each { |record| set_inverse_instance(record) } records.each { |record| set_inverse_instance(record) }
records records
end end
......
...@@ -196,7 +196,7 @@ def delete_through_records(records) ...@@ -196,7 +196,7 @@ def delete_through_records(records)
def find_target def find_target
return [] unless target_reflection_has_associated_record? return [] unless target_reflection_has_associated_record?
get_records super
end end
# NOTE - not sure that we can actually cope with inverses here # NOTE - not sure that we can actually cope with inverses here
......
...@@ -44,8 +44,8 @@ def create_scope ...@@ -44,8 +44,8 @@ def create_scope
scope.scope_for_create.stringify_keys.except(klass.primary_key) scope.scope_for_create.stringify_keys.except(klass.primary_key)
end end
def get_records def find_target
return scope.limit(1).records if skip_statement_cache? return scope.take if skip_statement_cache?
conn = klass.connection conn = klass.connection
sc = reflection.association_scope_cache(conn, owner) do sc = reflection.association_scope_cache(conn, owner) do
...@@ -56,11 +56,7 @@ def get_records ...@@ -56,11 +56,7 @@ def get_records
end end
binds = AssociationScope.get_bind_values(owner, reflection.chain) binds = AssociationScope.get_bind_values(owner, reflection.chain)
sc.execute binds, klass, klass.connection if record = sc.execute(binds, klass, conn).first
end
def find_target
if record = get_records.first
set_inverse_instance record set_inverse_instance record
end end
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册