Method: ActiveRecord::Reflection::BelongsToReflection#association_primary_key

Defined in:
activerecord/lib/active_record/reflection.rb

#association_primary_key(klass = nil) ⇒ Object

klass option is necessary to support loading polymorphic associations



926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
# File 'activerecord/lib/active_record/reflection.rb', line 926

def association_primary_key(klass = nil)
  if primary_key = options[:primary_key]
    @association_primary_key ||= if primary_key.is_a?(Array)
      primary_key.map { |pk| pk.to_s.freeze }.freeze
    else
      -primary_key.to_s
    end
  elsif (klass || self.klass).has_query_constraints? || options[:query_constraints]
    (klass || self.klass).composite_query_constraints_list
  elsif (klass || self.klass).composite_primary_key?
    # If klass has composite primary key of shape [:<tenant_key>, :id], infer primary_key as :id
    primary_key = (klass || self.klass).primary_key
    primary_key.include?("id") ? "id" : primary_key
  else
    primary_key(klass || self.klass)
  end
end