Class: ActiveRecord::Reflection::AssociationReflection
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
#active_record, #name, #options, #plural_name, #scope
Instance Method Summary
collapse
#==, #autosave=, #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_cached_counter?, #inverse_of, #inverse_updates_counter_in_memory?, #inverse_which_updates_counter_cache, #join_scope, #join_scopes, #klass_join_scope, #scopes, #strict_loading?, #table_name, #through_reflection?
Constructor Details
#initialize(name, scope, options, active_record) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
415
416
417
418
419
420
421
422
423
424
|
# File 'lib/active_record/reflection.rb', line 415
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]
@constructable = calculate_constructable(macro, options)
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_type ⇒ Object
Returns the value of attribute foreign_type.
412
413
414
|
# File 'lib/active_record/reflection.rb', line 412
def foreign_type
@foreign_type
end
|
#parent_reflection ⇒ Object
413
414
415
|
# File 'lib/active_record/reflection.rb', line 413
def parent_reflection
@parent_reflection
end
|
#type ⇒ Object
Returns the value of attribute type.
412
413
414
|
# File 'lib/active_record/reflection.rb', line 412
def type
@type
end
|
Instance Method Details
#active_record_primary_key ⇒ Object
454
455
456
|
# File 'lib/active_record/reflection.rb', line 454
def active_record_primary_key
@active_record_primary_key ||= -(options[:primary_key]&.to_s || primary_key(active_record))
end
|
#add_as_polymorphic_through(reflection, seed) ⇒ Object
573
574
575
|
# File 'lib/active_record/reflection.rb', line 573
def add_as_polymorphic_through(reflection, seed)
seed + [PolymorphicReflection.new(self, reflection)]
end
|
#add_as_source(seed) ⇒ Object
569
570
571
|
# File 'lib/active_record/reflection.rb', line 569
def add_as_source(seed)
seed
end
|
#add_as_through(seed) ⇒ Object
577
578
579
|
# File 'lib/active_record/reflection.rb', line 577
def add_as_through(seed)
seed + [self]
end
|
#association_class ⇒ Object
560
|
# File 'lib/active_record/reflection.rb', line 560
def association_class; raise NotImplementedError; end
|
#association_foreign_key ⇒ Object
446
447
448
|
# File 'lib/active_record/reflection.rb', line 446
def association_foreign_key
@association_foreign_key ||= -(options[:association_foreign_key]&.to_s || class_name.foreign_key)
end
|
#association_primary_key(klass = nil) ⇒ Object
450
451
452
|
# File 'lib/active_record/reflection.rb', line 450
def association_primary_key(klass = nil)
primary_key(klass || self.klass)
end
|
#association_scope_cache(klass, owner, &block) ⇒ Object
426
427
428
429
430
431
432
|
# File 'lib/active_record/reflection.rb', line 426
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.
555
|
# File 'lib/active_record/reflection.rb', line 555
def belongs_to?; false; end
|
#check_preloadable! ⇒ Object
Also known as:
check_eager_loadable!
470
471
472
473
474
475
476
477
478
479
480
|
# File 'lib/active_record/reflection.rb', line 470
def check_preloadable!
return unless scope
unless scope.arity == 0
raise ArgumentError, " The association scope '\#{name}' is instance dependent (the scope\n block takes an argument). Preloading instance dependent scopes is\n not supported.\n MSG\n end\nend\n".squish
|
#check_validity! ⇒ Object
466
467
468
|
# File 'lib/active_record/reflection.rb', line 466
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.
503
504
505
|
# File 'lib/active_record/reflection.rb', line 503
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.
497
498
499
|
# File 'lib/active_record/reflection.rb', line 497
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.
537
538
539
|
# File 'lib/active_record/reflection.rb', line 537
def collection?
false
end
|
#compute_class(name) ⇒ Object
405
406
407
408
409
410
|
# File 'lib/active_record/reflection.rb', line 405
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
434
435
436
|
# File 'lib/active_record/reflection.rb', line 434
def constructable?
@constructable
end
|
#extensions ⇒ Object
581
582
583
|
# File 'lib/active_record/reflection.rb', line 581
def extensions
Array(options[:extend])
end
|
#foreign_key ⇒ Object
442
443
444
|
# File 'lib/active_record/reflection.rb', line 442
def foreign_key
@foreign_key ||= -(options[:foreign_key]&.to_s || derive_foreign_key)
end
|
#has_inverse? ⇒ Boolean
515
516
517
|
# File 'lib/active_record/reflection.rb', line 515
def has_inverse?
inverse_name
end
|
#has_one? ⇒ Boolean
Returns true if self is a has_one reflection.
558
|
# File 'lib/active_record/reflection.rb', line 558
def has_one?; false; end
|
#has_scope? ⇒ Boolean
511
512
513
|
# File 'lib/active_record/reflection.rb', line 511
def has_scope?
scope
end
|
#join_foreign_key ⇒ Object
462
463
464
|
# File 'lib/active_record/reflection.rb', line 462
def join_foreign_key
active_record_primary_key
end
|
#join_id_for(owner) ⇒ Object
483
484
485
|
# File 'lib/active_record/reflection.rb', line 483
def join_id_for(owner)
owner[join_foreign_key]
end
|
#join_primary_key(klass = nil) ⇒ Object
458
459
460
|
# File 'lib/active_record/reflection.rb', line 458
def join_primary_key(klass = nil)
foreign_key
end
|
#join_table ⇒ Object
438
439
440
|
# File 'lib/active_record/reflection.rb', line 438
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
532
|
# File 'lib/active_record/reflection.rb', line 532
def macro; raise NotImplementedError; end
|
#nested? ⇒ Boolean
507
508
509
|
# File 'lib/active_record/reflection.rb', line 507
def nested?
false
end
|
#polymorphic? ⇒ Boolean
562
563
564
|
# File 'lib/active_record/reflection.rb', line 562
def polymorphic?
options[:polymorphic]
end
|
#polymorphic_inverse_of(associated_class) ⇒ Object
519
520
521
522
523
524
525
526
527
|
# File 'lib/active_record/reflection.rb', line 519
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_reflection ⇒ Object
491
492
493
|
# File 'lib/active_record/reflection.rb', line 491
def source_reflection
self
end
|
#through_reflection ⇒ Object
487
488
489
|
# File 'lib/active_record/reflection.rb', line 487
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
550
551
552
|
# File 'lib/active_record/reflection.rb', line 550
def validate?
!options[:validate].nil? ? options[:validate] : (options[:autosave] == true || collection?)
end
|