diff options
Diffstat (limited to 'activerecord/lib/active_record/reflection.rb')
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 37 |
1 files changed, 13 insertions, 24 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index efe56454d0..d2b85e168b 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require "thread" require "active_support/core_ext/string/filters" require "active_support/deprecation" +require "concurrent/map" module ActiveRecord # = Active Record Reflection @@ -305,13 +305,17 @@ module ActiveRecord end def get_join_keys(association_klass) - JoinKeys.new(join_pk(association_klass), join_fk) + JoinKeys.new(join_pk(association_klass), join_foreign_key) end def build_scope(table, predicate_builder = predicate_builder(table)) Relation.create(klass, table, predicate_builder) end + def join_foreign_key + active_record_primary_key + end + protected def actual_source_reflection # FIXME: this is a horrible name self @@ -325,10 +329,6 @@ module ActiveRecord def join_pk(_) foreign_key end - - def join_fk - active_record_primary_key - end end # Base class for AggregateReflection and AssociationReflection. Objects of @@ -443,8 +443,7 @@ module ActiveRecord @type = options[:as] && (options[:foreign_type] || "#{options[:as]}_type") @foreign_type = options[:foreign_type] || "#{name}_type" @constructable = calculate_constructable(macro, options) - @association_scope_cache = {} - @scope_lock = Mutex.new + @association_scope_cache = Concurrent::Map.new if options[:class_name] && options[:class_name].class == Class ActiveSupport::Deprecation.warn(<<-MSG.squish) @@ -458,14 +457,12 @@ module ActiveRecord end end - def association_scope_cache(conn, owner) + def association_scope_cache(conn, owner, &block) key = conn.prepared_statements if polymorphic? key = [key, owner._read_attribute(@foreign_type)] end - @association_scope_cache[key] ||= @scope_lock.synchronize { - @association_scope_cache[key] ||= yield - } + @association_scope_cache.compute_if_absent(key) { StatementCache.create(conn, &block) } end def constructable? # :nodoc: @@ -757,16 +754,16 @@ module ActiveRecord owner[foreign_key] end + def join_foreign_key + foreign_key + end + private def calculate_constructable(macro, options) !polymorphic? end - def join_fk - foreign_key - end - def join_pk(klass) polymorphic? ? association_primary_key(klass) : association_primary_key end @@ -1076,10 +1073,6 @@ module ActiveRecord @reflection.scope end - def table_name - @reflection.table_name - end - def plural_name @reflection.plural_name end @@ -1115,10 +1108,6 @@ module ActiveRecord @association.klass end - def table_name - klass.table_name - end - def constraints @reflection.constraints end |