Class: ActiveRecord::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- AbstractReflection
- MacroReflection
- ActiveRecord::Reflection::AssociationReflection
- Defined in:
- activerecord/lib/active_record/reflection.rb
Overview
Holds all the metadata about an association as it was specified in the Active Record class.
Direct Known Subclasses
BelongsToReflection, HasAndBelongsToManyReflection, HasManyReflection, HasOneReflection
Instance Attribute Summary collapse
-
#foreign_type ⇒ Object
readonly
Returns the value of attribute foreign_type.
-
#parent_reflection ⇒ Object
Reflection.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from MacroReflection
#active_record, #name, #options, #plural_name, #scope
Instance Method Summary collapse
- #active_record_primary_key ⇒ Object
- #add_as_polymorphic_through(reflection, seed) ⇒ Object
- #add_as_source(seed) ⇒ Object
- #add_as_through(seed) ⇒ Object
- #association_class ⇒ Object
- #association_foreign_key ⇒ Object
- #association_primary_key(klass = nil) ⇒ Object
- #association_scope_cache(klass, owner, &block) ⇒ Object
-
#belongs_to? ⇒ Boolean
Returns
true
ifself
is abelongs_to
reflection. - #check_eager_loadable! ⇒ Object
- #check_validity! ⇒ Object
-
#clear_association_scope_cache ⇒ Object
This is for clearing cache on the reflection.
-
#collect_join_chain ⇒ Object
A chain of reflections from this one back to the owner.
-
#collection? ⇒ Boolean
Returns whether or not this association reflection is for a collection association.
-
#compute_class(name) ⇒ Object
:nodoc:.
- #extensions ⇒ Object
- #foreign_key ⇒ Object
- #has_inverse? ⇒ Boolean
-
#has_one? ⇒ Boolean
Returns
true
ifself
is ahas_one
reflection. - #has_scope? ⇒ Boolean
-
#initialize(name, scope, options, active_record) ⇒ AssociationReflection
constructor
A new instance of AssociationReflection.
- #join_foreign_key ⇒ Object
-
#join_id_for(owner) ⇒ Object
:nodoc:.
- #join_primary_key(klass = nil) ⇒ Object
- #join_table ⇒ Object
-
#macro ⇒ Object
Returns the macro type.
- #nested? ⇒ Boolean
- #polymorphic? ⇒ Boolean
- #polymorphic_inverse_of(associated_class) ⇒ Object
- #source_reflection ⇒ Object
- #through_reflection ⇒ Object
-
#validate? ⇒ Boolean
Returns whether or not the association should be validated as part of the parent’s validation.
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?, #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.
472 473 474 475 476 477 478 479 480 481 482 |
# File 'activerecord/lib/active_record/reflection.rb', line 472 def initialize(name, scope, , active_record) super @type = -([:foreign_type]&.to_s || "#{[:as]}_type") if [:as] @foreign_type = -([:foreign_type]&.to_s || "#{name}_type") if [:polymorphic] @join_table = nil @foreign_key = nil @association_foreign_key = nil @association_primary_key = nil ensure_option_not_given_as_class!(:class_name) end |
Instance Attribute Details
#foreign_type ⇒ Object (readonly)
Returns the value of attribute foreign_type
469 470 471 |
# File 'activerecord/lib/active_record/reflection.rb', line 469 def foreign_type @foreign_type end |
#parent_reflection ⇒ Object
Reflection
470 471 472 |
# File 'activerecord/lib/active_record/reflection.rb', line 470 def parent_reflection @parent_reflection end |
#type ⇒ Object (readonly)
Returns the value of attribute type
469 470 471 |
# File 'activerecord/lib/active_record/reflection.rb', line 469 def type @type end |
Instance Method Details
#active_record_primary_key ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'activerecord/lib/active_record/reflection.rb', line 513 def active_record_primary_key custom_primary_key = [: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 [:query_constraints] active_record.query_constraints_list else primary_key(active_record).freeze end end |
#add_as_polymorphic_through(reflection, seed) ⇒ Object
639 640 641 |
# File 'activerecord/lib/active_record/reflection.rb', line 639 def add_as_polymorphic_through(reflection, seed) seed + [PolymorphicReflection.new(self, reflection)] end |
#add_as_source(seed) ⇒ Object
635 636 637 |
# File 'activerecord/lib/active_record/reflection.rb', line 635 def add_as_source(seed) seed end |
#add_as_through(seed) ⇒ Object
643 644 645 |
# File 'activerecord/lib/active_record/reflection.rb', line 643 def add_as_through(seed) seed + [self] end |
#association_class ⇒ Object
629 |
# File 'activerecord/lib/active_record/reflection.rb', line 629 def association_class; raise NotImplementedError; end |
#association_foreign_key ⇒ Object
505 506 507 |
# File 'activerecord/lib/active_record/reflection.rb', line 505 def association_foreign_key @association_foreign_key ||= -([:association_foreign_key]&.to_s || class_name.foreign_key) end |
#association_primary_key(klass = nil) ⇒ Object
509 510 511 |
# File 'activerecord/lib/active_record/reflection.rb', line 509 def association_primary_key(klass = nil) primary_key(klass || self.klass) end |
#association_scope_cache(klass, owner, &block) ⇒ Object
484 485 486 487 488 489 490 |
# File 'activerecord/lib/active_record/reflection.rb', line 484 def association_scope_cache(klass, owner, &block) key = self if polymorphic? key = [key, owner._read_attribute(@foreign_type)] end klass.cached_find_by_statement(key, &block) end |
#belongs_to? ⇒ Boolean
Returns true
if self
is a belongs_to
reflection.
624 |
# File 'activerecord/lib/active_record/reflection.rb', line 624 def belongs_to?; false; end |
#check_eager_loadable! ⇒ Object
540 541 542 543 544 545 546 547 548 549 550 |
# File 'activerecord/lib/active_record/reflection.rb', line 540 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
536 537 538 |
# File 'activerecord/lib/active_record/reflection.rb', line 536 def check_validity! check_validity_of_inverse! 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.
572 573 574 |
# File 'activerecord/lib/active_record/reflection.rb', line 572 def clear_association_scope_cache # :nodoc: 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.
566 567 568 |
# File 'activerecord/lib/active_record/reflection.rb', line 566 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.
606 607 608 |
# File 'activerecord/lib/active_record/reflection.rb', line 606 def collection? false end |
#compute_class(name) ⇒ Object
:nodoc:
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'activerecord/lib/active_record/reflection.rb', line 445 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/) = "Missing model class #{name} for the #{active_record}##{self.name} association." += " You can specify a different model class with the :class_name option." unless [:class_name] raise NameError.new(, 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
647 648 649 |
# File 'activerecord/lib/active_record/reflection.rb', line 647 def extensions Array([:extend]) end |
#foreign_key ⇒ Object
496 497 498 499 500 501 502 503 |
# File 'activerecord/lib/active_record/reflection.rb', line 496 def foreign_key @foreign_key ||= if [:query_constraints] # composite foreign keys support [:query_constraints].map { |fk| fk.to_s.freeze }.freeze else -([:foreign_key]&.to_s || derive_foreign_key) end end |
#has_inverse? ⇒ Boolean
584 585 586 |
# File 'activerecord/lib/active_record/reflection.rb', line 584 def has_inverse? inverse_name end |
#has_one? ⇒ Boolean
Returns true
if self
is a has_one
reflection.
627 |
# File 'activerecord/lib/active_record/reflection.rb', line 627 def has_one?; false; end |
#has_scope? ⇒ Boolean
580 581 582 |
# File 'activerecord/lib/active_record/reflection.rb', line 580 def has_scope? scope end |
#join_foreign_key ⇒ Object
532 533 534 |
# File 'activerecord/lib/active_record/reflection.rb', line 532 def join_foreign_key active_record_primary_key end |
#join_id_for(owner) ⇒ Object
:nodoc:
552 553 554 |
# File 'activerecord/lib/active_record/reflection.rb', line 552 def join_id_for(owner) # :nodoc: Array(join_foreign_key).map { |key| owner[key] } end |
#join_primary_key(klass = nil) ⇒ Object
528 529 530 |
# File 'activerecord/lib/active_record/reflection.rb', line 528 def join_primary_key(klass = nil) foreign_key end |
#join_table ⇒ Object
492 493 494 |
# File 'activerecord/lib/active_record/reflection.rb', line 492 def join_table @join_table ||= -([:join_table]&.to_s || derive_join_table) end |
#macro ⇒ Object
Returns the macro type.
has_many :clients
returns :has_many
601 |
# File 'activerecord/lib/active_record/reflection.rb', line 601 def macro; raise NotImplementedError; end |
#nested? ⇒ Boolean
576 577 578 |
# File 'activerecord/lib/active_record/reflection.rb', line 576 def nested? false end |
#polymorphic? ⇒ Boolean
631 632 633 |
# File 'activerecord/lib/active_record/reflection.rb', line 631 def polymorphic? [:polymorphic] end |
#polymorphic_inverse_of(associated_class) ⇒ Object
588 589 590 591 592 593 594 595 596 |
# File 'activerecord/lib/active_record/reflection.rb', line 588 def polymorphic_inverse_of(associated_class) if has_inverse? if inverse_relationship = associated_class._reflect_on_association([:inverse_of]) inverse_relationship else raise InverseOfAssociationNotFoundError.new(self, associated_class) end end end |
#source_reflection ⇒ Object
560 561 562 |
# File 'activerecord/lib/active_record/reflection.rb', line 560 def source_reflection self end |
#through_reflection ⇒ Object
556 557 558 |
# File 'activerecord/lib/active_record/reflection.rb', line 556 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
619 620 621 |
# File 'activerecord/lib/active_record/reflection.rb', line 619 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || collection?) end |