mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
use statement cache for belongs_to relations
This commit is contained in:
parent
bdd64912ac
commit
86d6f05e69
3 changed files with 17 additions and 3 deletions
|
@ -416,7 +416,7 @@ module ActiveRecord
|
|||
return scope.to_a if reflection.scope_chain.any?(&:any?)
|
||||
|
||||
conn = klass.connection
|
||||
sc = reflection.association_scope_cache(conn) do
|
||||
sc = reflection.association_scope_cache(conn, owner) do
|
||||
StatementCache.create(conn) { |params|
|
||||
as = AssociationScope.create { params.bind }
|
||||
target_scope.merge as.scope(self, conn)
|
||||
|
|
|
@ -39,7 +39,18 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def get_records
|
||||
scope.limit(1).to_a
|
||||
return scope.limit(1).to_a if reflection.scope_chain.any?(&:any?)
|
||||
|
||||
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)).limit(1)
|
||||
}
|
||||
end
|
||||
|
||||
binds = AssociationScope.get_bind_values(owner, reflection.chain)
|
||||
sc.execute binds, klass, klass.connection
|
||||
end
|
||||
|
||||
def find_target
|
||||
|
|
|
@ -205,8 +205,11 @@ module ActiveRecord
|
|||
@scope_lock = Mutex.new
|
||||
end
|
||||
|
||||
def association_scope_cache(conn)
|
||||
def association_scope_cache(conn, owner)
|
||||
key = conn.prepared_statements
|
||||
if options[:polymorphic]
|
||||
key = [key, owner.read_attribute(@foreign_type)]
|
||||
end
|
||||
@association_scope_cache[key] ||= @scope_lock.synchronize {
|
||||
@association_scope_cache[key] ||= yield
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue