Class: ActiveRecord::Reflection::AssociationReflection

Inherits:
MacroReflection show all
Defined in:
lib/active_record/reflection.rb

Overview

Holds all the metadata about an association as it was specified in the Active Record class.

Constant Summary collapse

VALID_AUTOMATIC_INVERSE_MACROS =
[:has_many, :has_one, :belongs_to]
INVALID_AUTOMATIC_INVERSE_OPTIONS =
[:through, :foreign_key]

Instance Attribute Summary collapse

Attributes inherited from MacroReflection

#active_record, #name, #options, #plural_name, #scope

Instance Method Summary collapse

Methods inherited from MacroReflection

#==, #autosave=, #klass, #scope_for

Methods inherited from AbstractReflection

#alias_candidate, #build_association, #build_scope, #chain, #check_validity_of_inverse!, #class_name, #constraints, #counter_cache_column, #counter_must_be_updated_by_has_many?, #get_join_keys, #has_cached_counter?, #inverse_of, #inverse_updates_counter_in_memory?, #inverse_which_updates_counter_cache, #join_foreign_key, #join_keys, #join_primary_key, #join_scope, #join_scopes, #klass_join_scope, #scopes, #table_name, #through_reflection?

Constructor Details

#initialize(name, scope, options, active_record) ⇒ AssociationReflection

Returns a new instance of AssociationReflection.



430
431
432
433
434
435
436
437
438
439
440
# File 'lib/active_record/reflection.rb', line 430

def initialize(name, scope, options, active_record)
  super
  @type         = options[:as] && (options[:foreign_type] || "#{options[:as]}_type")
  @foreign_type = options[:polymorphic] && (options[:foreign_type] || "#{name}_type")
  @constructable = calculate_constructable(macro, options)
  @association_scope_cache = Concurrent::Map.new

  if options[:class_name] && options[:class_name].class == Class
    raise ArgumentError, "A class was passed to `:class_name` but we are expecting a string."
  end
end

Instance Attribute Details

#foreign_typeObject (readonly)

Returns the value of attribute foreign_type.



427
428
429
# File 'lib/active_record/reflection.rb', line 427

def foreign_type
  @foreign_type
end

#parent_reflectionObject

Reflection



428
429
430
# File 'lib/active_record/reflection.rb', line 428

def parent_reflection
  @parent_reflection
end

#typeObject (readonly)

Returns the value of attribute type.



427
428
429
# File 'lib/active_record/reflection.rb', line 427

def type
  @type
end

Instance Method Details

#active_record_primary_keyObject



471
472
473
# File 'lib/active_record/reflection.rb', line 471

def active_record_primary_key
  @active_record_primary_key ||= options[:primary_key] || primary_key(active_record)
end

#add_as_polymorphic_through(reflection, seed) ⇒ Object



582
583
584
# File 'lib/active_record/reflection.rb', line 582

def add_as_polymorphic_through(reflection, seed)
  seed + [PolymorphicReflection.new(self, reflection)]
end

#add_as_source(seed) ⇒ Object



578
579
580
# File 'lib/active_record/reflection.rb', line 578

def add_as_source(seed)
  seed
end

#add_as_through(seed) ⇒ Object



586
587
588
# File 'lib/active_record/reflection.rb', line 586

def add_as_through(seed)
  seed + [self]
end

#association_classObject

Raises:

  • (NotImplementedError)


569
# File 'lib/active_record/reflection.rb', line 569

def association_class; raise NotImplementedError; end

#association_foreign_keyObject



462
463
464
# File 'lib/active_record/reflection.rb', line 462

def association_foreign_key
  @association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key
end

#association_primary_key(klass = nil) ⇒ Object

klass option is necessary to support loading polymorphic associations



467
468
469
# File 'lib/active_record/reflection.rb', line 467

def association_primary_key(klass = nil)
  options[:primary_key] || primary_key(klass || self.klass)
end

#association_scope_cache(conn, owner, &block) ⇒ Object



442
443
444
445
446
447
448
# File 'lib/active_record/reflection.rb', line 442

def association_scope_cache(conn, owner, &block)
  key = conn.prepared_statements
  if polymorphic?
    key = [key, owner._read_attribute(@foreign_type)]
  end
  @association_scope_cache.compute_if_absent(key) { StatementCache.create(conn, &block) }
end

#belongs_to?Boolean

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


564
# File 'lib/active_record/reflection.rb', line 564

def belongs_to?; false; end

#check_preloadable!Object Also known as: check_eager_loadable!



479
480
481
482
483
484
485
486
487
488
489
# File 'lib/active_record/reflection.rb', line 479

def check_preloadable!
  return unless scope

  unless scope.arity == 0
    raise ArgumentError, <<-MSG.squish
      The association scope '#{name}' is instance dependent (the scope
      block takes an argument). Preloading instance dependent scopes is
      not supported.
    MSG
  end
end

#check_validity!Object



475
476
477
# File 'lib/active_record/reflection.rb', line 475

def check_validity!
  check_validity_of_inverse!
end

#clear_association_scope_cacheObject

This is for clearing cache on the reflection. Useful for tests that need to compare SQL queries on associations.



512
513
514
# File 'lib/active_record/reflection.rb', line 512

def clear_association_scope_cache # :nodoc:
  @association_scope_cache.clear
end

#collect_join_chainObject

A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.



506
507
508
# File 'lib/active_record/reflection.rb', line 506

def collect_join_chain
  [self]
end

#collection?Boolean

Returns whether or not this association reflection is for a collection association. Returns true if the macro is either has_many or has_and_belongs_to_many, false otherwise.

Returns:

  • (Boolean)


546
547
548
# File 'lib/active_record/reflection.rb', line 546

def collection?
  false
end

#compute_class(name) ⇒ Object

:nodoc:



420
421
422
423
424
425
# File 'lib/active_record/reflection.rb', line 420

def compute_class(name)
  if polymorphic?
    raise ArgumentError, "Polymorphic associations do not support computing the class."
  end
  active_record.send(:compute_type, name)
end

#constructable?Boolean

:nodoc:

Returns:

  • (Boolean)


450
451
452
# File 'lib/active_record/reflection.rb', line 450

def constructable? # :nodoc:
  @constructable
end

#extensionsObject



590
591
592
# File 'lib/active_record/reflection.rb', line 590

def extensions
  Array(options[:extend])
end

#foreign_keyObject



458
459
460
# File 'lib/active_record/reflection.rb', line 458

def foreign_key
  @foreign_key ||= options[:foreign_key] || derive_foreign_key.freeze
end

#has_inverse?Boolean

Returns:

  • (Boolean)


524
525
526
# File 'lib/active_record/reflection.rb', line 524

def has_inverse?
  inverse_name
end

#has_one?Boolean

Returns true if self is a has_one reflection.

Returns:

  • (Boolean)


567
# File 'lib/active_record/reflection.rb', line 567

def has_one?; false; end

#has_scope?Boolean

Returns:

  • (Boolean)


520
521
522
# File 'lib/active_record/reflection.rb', line 520

def has_scope?
  scope
end

#join_id_for(owner) ⇒ Object

:nodoc:



492
493
494
# File 'lib/active_record/reflection.rb', line 492

def join_id_for(owner) # :nodoc:
  owner[join_foreign_key]
end

#join_tableObject



454
455
456
# File 'lib/active_record/reflection.rb', line 454

def join_table
  @join_table ||= options[:join_table] || derive_join_table
end

#macroObject

Returns the macro type.

has_many :clients returns :has_many

Raises:

  • (NotImplementedError)


541
# File 'lib/active_record/reflection.rb', line 541

def macro; raise NotImplementedError; end

#nested?Boolean

Returns:

  • (Boolean)


516
517
518
# File 'lib/active_record/reflection.rb', line 516

def nested?
  false
end

#polymorphic?Boolean

Returns:

  • (Boolean)


571
572
573
# File 'lib/active_record/reflection.rb', line 571

def polymorphic?
  options[:polymorphic]
end

#polymorphic_inverse_of(associated_class) ⇒ Object



528
529
530
531
532
533
534
535
536
# File 'lib/active_record/reflection.rb', line 528

def polymorphic_inverse_of(associated_class)
  if has_inverse?
    if inverse_relationship = associated_class._reflect_on_association(options[:inverse_of])
      inverse_relationship
    else
      raise InverseOfAssociationNotFoundError.new(self, associated_class)
    end
  end
end

#source_reflectionObject



500
501
502
# File 'lib/active_record/reflection.rb', line 500

def source_reflection
  self
end

#through_reflectionObject



496
497
498
# File 'lib/active_record/reflection.rb', line 496

def through_reflection
  nil
end

#validate?Boolean

Returns whether or not the association should be validated as part of the parent’s validation.

Unless you explicitly disable validation with validate: false, validation will take place when:

  • you explicitly enable validation; validate: true

  • you use autosave; autosave: true

  • the association is a has_many association

Returns:

  • (Boolean)


559
560
561
# File 'lib/active_record/reflection.rb', line 559

def validate?
  !options[:validate].nil? ? options[:validate] : (options[:autosave] == true || collection?)
end