Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use statement cache for belongs_to relations

  • Loading branch information...
commit 86d6f05e695c545b7e744c958c8d740d0cdf5486 1 parent bdd6491
@tenderlove tenderlove authored
View
2  activerecord/lib/active_record/associations/collection_association.rb
@@ -416,7 +416,7 @@ def get_records
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)
View
13 activerecord/lib/active_record/associations/singular_association.rb
@@ -39,7 +39,18 @@ def create_scope
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
View
5 activerecord/lib/active_record/reflection.rb
@@ -205,8 +205,11 @@ def initialize(macro, name, scope, options, active_record)
@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
}
Please sign in to comment.
Something went wrong with that request. Please try again.