Class: ActiveRecord::Reflection::AssociationReflection
Overview
Holds all the metadata about an association as it was specified in the Active Record class.
Instance Attribute Summary collapse
#active_record, #name, #options, #plural_name, #scope
Instance Method Summary
collapse
#==, #autosave=, #compute_name, #klass, #scope_for
#alias_candidate, #build_association, #build_scope, #chain, #check_validity_of_inverse!, #class_name, #constraints, #counter_cache_column, #counter_must_be_updated_by_has_many?, #has_active_cached_counter?, #has_cached_counter?, #inverse_of, #inverse_updates_counter_in_memory?, #inverse_which_updates_counter_cache, #join_scope, #join_scopes, #klass_join_scope, #scopes, #strict_loading?, #strict_loading_violation_message, #table_name, #through_reflection?
Constructor Details
#initialize(name, scope, options, active_record) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
# File 'lib/active_record/reflection.rb', line 521
def initialize(name, scope, options, active_record)
super
@type = -(options[:foreign_type]&.to_s || "#{options[:as]}_type") if options[:as]
@foreign_type = -(options[:foreign_type]&.to_s || "#{name}_type") if options[:polymorphic]
@join_table = nil
@foreign_key = nil
@association_foreign_key = nil
@association_primary_key = nil
if options[:query_constraints]
ActiveRecord.deprecator.warn <<~MSG.squish
Setting `query_constraints:` option on `#{active_record}.#{macro} :#{name}` is deprecated.
To maintain current behavior, use the `foreign_key` option instead.
MSG
end
if options[:foreign_key].is_a?(Array)
options[:query_constraints] = options.delete(:foreign_key)
end
ensure_option_not_given_as_class!(:class_name)
end
|
Instance Attribute Details
#foreign_type ⇒ Object
Returns the value of attribute foreign_type.
518
519
520
|
# File 'lib/active_record/reflection.rb', line 518
def foreign_type
@foreign_type
end
|
#parent_reflection ⇒ Object
519
520
521
|
# File 'lib/active_record/reflection.rb', line 519
def parent_reflection
@parent_reflection
end
|
#type ⇒ Object
Returns the value of attribute type.
518
519
520
|
# File 'lib/active_record/reflection.rb', line 518
def type
@type
end
|
Instance Method Details
#active_record_primary_key ⇒ Object
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
# File 'lib/active_record/reflection.rb', line 586
def active_record_primary_key
custom_primary_key = options[:primary_key]
@active_record_primary_key ||= if custom_primary_key
if custom_primary_key.is_a?(Array)
custom_primary_key.map { |pk| pk.to_s.freeze }.freeze
else
custom_primary_key.to_s.freeze
end
elsif active_record.has_query_constraints? || options[:query_constraints]
active_record.query_constraints_list
elsif active_record.composite_primary_key?
primary_key = primary_key(active_record)
primary_key.include?("id") ? "id" : primary_key.freeze
else
primary_key(active_record).freeze
end
end
|
#add_as_polymorphic_through(reflection, seed) ⇒ Object
732
733
734
|
# File 'lib/active_record/reflection.rb', line 732
def add_as_polymorphic_through(reflection, seed)
seed + [PolymorphicReflection.new(self, reflection)]
end
|
#add_as_source(seed) ⇒ Object
728
729
730
|
# File 'lib/active_record/reflection.rb', line 728
def add_as_source(seed)
seed
end
|
#add_as_through(seed) ⇒ Object
736
737
738
|
# File 'lib/active_record/reflection.rb', line 736
def add_as_through(seed)
seed + [self]
end
|
#association_class ⇒ Object
718
|
# File 'lib/active_record/reflection.rb', line 718
def association_class; raise NotImplementedError; end
|
#association_foreign_key ⇒ Object
578
579
580
|
# File 'lib/active_record/reflection.rb', line 578
def association_foreign_key
@association_foreign_key ||= -(options[:association_foreign_key]&.to_s || class_name.foreign_key)
end
|
#association_primary_key(klass = nil) ⇒ Object
582
583
584
|
# File 'lib/active_record/reflection.rb', line 582
def association_primary_key(klass = nil)
primary_key(klass || self.klass)
end
|
#association_scope_cache(klass, owner, &block) ⇒ Object
544
545
546
547
548
549
550
551
552
|
# File 'lib/active_record/reflection.rb', line 544
def association_scope_cache(klass, owner, &block)
key = self
if polymorphic?
key = [key, owner._read_attribute(@foreign_type)]
end
klass.with_connection do |connection|
klass.cached_find_by_statement(connection, key, &block)
end
end
|
#belongs_to? ⇒ Boolean
Returns true
if self
is a belongs_to
reflection.
713
|
# File 'lib/active_record/reflection.rb', line 713
def belongs_to?; false; end
|
#check_eager_loadable! ⇒ Object
629
630
631
632
633
634
635
636
637
638
639
|
# File 'lib/active_record/reflection.rb', line 629
def check_eager_loadable!
return unless scope
unless scope.arity == 0
raise ArgumentError, <<-MSG.squish
The association scope '#{name}' is instance dependent (the scope
block takes an argument). Eager loading instance dependent scopes
is not supported.
MSG
end
end
|
#check_validity! ⇒ Object
617
618
619
620
621
622
623
624
625
626
627
|
# File 'lib/active_record/reflection.rb', line 617
def check_validity!
check_validity_of_inverse!
if !polymorphic? && (klass.composite_primary_key? || active_record.composite_primary_key?)
if (has_one? || collection?) && Array(active_record_primary_key).length != Array(foreign_key).length
raise CompositePrimaryKeyMismatchError.new(self)
elsif belongs_to? && Array(association_primary_key).length != Array(foreign_key).length
raise CompositePrimaryKeyMismatchError.new(self)
end
end
end
|
#clear_association_scope_cache ⇒ Object
This is for clearing cache on the reflection. Useful for tests that need to compare SQL queries on associations.
661
662
663
|
# File 'lib/active_record/reflection.rb', line 661
def clear_association_scope_cache klass.initialize_find_by_cache
end
|
#collect_join_chain ⇒ Object
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
655
656
657
|
# File 'lib/active_record/reflection.rb', line 655
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.
695
696
697
|
# File 'lib/active_record/reflection.rb', line 695
def collection?
false
end
|
#compute_class(name) ⇒ Object
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
# File 'lib/active_record/reflection.rb', line 494
def compute_class(name)
if polymorphic?
raise ArgumentError, "Polymorphic associations do not support computing the class."
end
begin
klass = active_record.send(:compute_type, name)
rescue NameError => error
if error.name.match?(/(?:\A|::)#{name}\z/)
message = "Missing model class #{name} for the #{active_record}##{self.name} association."
message += " You can specify a different model class with the :class_name option." unless options[:class_name]
raise NameError.new(message, name)
else
raise
end
end
unless klass < ActiveRecord::Base
raise ArgumentError, "The #{name} model class for the #{active_record}##{self.name} association is not an ActiveRecord::Base subclass."
end
klass
end
|
#extensions ⇒ Object
740
741
742
|
# File 'lib/active_record/reflection.rb', line 740
def extensions
Array(options[:extend])
end
|
#foreign_key(infer_from_inverse_of: true) ⇒ Object
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
# File 'lib/active_record/reflection.rb', line 558
def foreign_key(infer_from_inverse_of: true)
@foreign_key ||= if options[:foreign_key]
if options[:foreign_key].is_a?(Array)
options[:foreign_key].map { |fk| fk.to_s.freeze }.freeze
else
options[:foreign_key].to_s.freeze
end
elsif options[:query_constraints]
options[:query_constraints].map { |fk| fk.to_s.freeze }.freeze
else
derived_fk = derive_foreign_key(infer_from_inverse_of: infer_from_inverse_of)
if active_record.has_query_constraints?
derived_fk = derive_fk_query_constraints(derived_fk)
end
derived_fk
end
end
|
#has_inverse? ⇒ Boolean
673
674
675
|
# File 'lib/active_record/reflection.rb', line 673
def has_inverse?
inverse_name
end
|
#has_one? ⇒ Boolean
Returns true
if self
is a has_one
reflection.
716
|
# File 'lib/active_record/reflection.rb', line 716
def has_one?; false; end
|
#has_scope? ⇒ Boolean
669
670
671
|
# File 'lib/active_record/reflection.rb', line 669
def has_scope?
scope
end
|
#join_foreign_key ⇒ Object
613
614
615
|
# File 'lib/active_record/reflection.rb', line 613
def join_foreign_key
active_record_primary_key
end
|
#join_id_for(owner) ⇒ Object
641
642
643
|
# File 'lib/active_record/reflection.rb', line 641
def join_id_for(owner) Array(join_foreign_key).map { |key| owner._read_attribute(key) }
end
|
#join_primary_key(klass = nil) ⇒ Object
605
606
607
|
# File 'lib/active_record/reflection.rb', line 605
def join_primary_key(klass = nil)
foreign_key
end
|
#join_primary_type ⇒ Object
609
610
611
|
# File 'lib/active_record/reflection.rb', line 609
def join_primary_type
type
end
|
#join_table ⇒ Object
554
555
556
|
# File 'lib/active_record/reflection.rb', line 554
def join_table
@join_table ||= -(options[:join_table]&.to_s || derive_join_table)
end
|
#macro ⇒ Object
Returns the macro type.
has_many :clients
returns :has_many
690
|
# File 'lib/active_record/reflection.rb', line 690
def macro; raise NotImplementedError; end
|
#nested? ⇒ Boolean
665
666
667
|
# File 'lib/active_record/reflection.rb', line 665
def nested?
false
end
|
#polymorphic? ⇒ Boolean
720
721
722
|
# File 'lib/active_record/reflection.rb', line 720
def polymorphic?
options[:polymorphic]
end
|
#polymorphic_inverse_of(associated_class) ⇒ Object
677
678
679
680
681
682
683
684
685
|
# File 'lib/active_record/reflection.rb', line 677
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
|
#polymorphic_name ⇒ Object
724
725
726
|
# File 'lib/active_record/reflection.rb', line 724
def polymorphic_name
active_record.polymorphic_name
end
|
#source_reflection ⇒ Object
649
650
651
|
# File 'lib/active_record/reflection.rb', line 649
def source_reflection
self
end
|
#through_reflection ⇒ Object
645
646
647
|
# File 'lib/active_record/reflection.rb', line 645
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
708
709
710
|
# File 'lib/active_record/reflection.rb', line 708
def validate?
!options[:validate].nil? ? options[:validate] : (options[:autosave] == true || collection?)
end
|