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
Constant Summary collapse
- VALID_AUTOMATIC_INVERSE_MACROS =
[:has_many, :has_one, :belongs_to]
- INVALID_AUTOMATIC_INVERSE_OPTIONS =
[:conditions, :through, :foreign_key]
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
klass option is necessary to support loading polymorphic associations.
- #association_primary_key_type ⇒ Object
- #association_scope_cache(conn, owner, &block) ⇒ Object
-
#belongs_to? ⇒ Boolean
Returns
true
ifself
is abelongs_to
reflection. - #check_preloadable! ⇒ Object (also: #check_eager_loadable!)
- #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
-
#constructable? ⇒ Boolean
: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_id_for(owner) ⇒ Object
:nodoc:.
- #join_table ⇒ Object
-
#klass ⇒ Object
Returns the target association’s class.
-
#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
Methods inherited from AbstractReflection
#alias_candidate, #build_association, #build_join_constraint, #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_scope, #join_scopes, #klass_join_scope, #primary_key_type, #quoted_table_name, #scope_chain, #scopes, #table_name, #through_reflection?
Constructor Details
#initialize(name, scope, options, active_record) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'activerecord/lib/active_record/reflection.rb', line 439 def initialize(name, scope, , active_record) super @automatic_inverse_of = nil @type = [:as] && ([:foreign_type] || "#{[:as]}_type") @foreign_type = [:foreign_type] || "#{name}_type" @constructable = calculate_constructable(macro, ) @association_scope_cache = Concurrent::Map.new if [:class_name] && [:class_name].class == Class ActiveSupport::Deprecation.warn(<<-MSG.squish) Passing a class to the `class_name` is deprecated and will raise an ArgumentError in Quails 5.2. It eagerloads more classes than necessary and potentially creates circular dependencies. Please pass the class name as a string: `#{macro} :#{name}, class_name: '#{[:class_name]}'` MSG end end |
Instance Attribute Details
#foreign_type ⇒ Object (readonly)
Returns the value of attribute foreign_type.
436 437 438 |
# File 'activerecord/lib/active_record/reflection.rb', line 436 def foreign_type @foreign_type end |
#parent_reflection ⇒ Object
Reflection
437 438 439 |
# File 'activerecord/lib/active_record/reflection.rb', line 437 def parent_reflection @parent_reflection end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
436 437 438 |
# File 'activerecord/lib/active_record/reflection.rb', line 436 def type @type end |
Instance Method Details
#active_record_primary_key ⇒ Object
492 493 494 |
# File 'activerecord/lib/active_record/reflection.rb', line 492 def active_record_primary_key @active_record_primary_key ||= [:primary_key] || primary_key(active_record) end |
#add_as_polymorphic_through(reflection, seed) ⇒ Object
603 604 605 |
# File 'activerecord/lib/active_record/reflection.rb', line 603 def add_as_polymorphic_through(reflection, seed) seed + [PolymorphicReflection.new(self, reflection)] end |
#add_as_source(seed) ⇒ Object
599 600 601 |
# File 'activerecord/lib/active_record/reflection.rb', line 599 def add_as_source(seed) seed end |
#add_as_through(seed) ⇒ Object
607 608 609 |
# File 'activerecord/lib/active_record/reflection.rb', line 607 def add_as_through(seed) seed + [self] end |
#association_class ⇒ Object
590 |
# File 'activerecord/lib/active_record/reflection.rb', line 590 def association_class; raise NotImplementedError; end |
#association_foreign_key ⇒ Object
479 480 481 |
# File 'activerecord/lib/active_record/reflection.rb', line 479 def association_foreign_key @association_foreign_key ||= [:association_foreign_key] || class_name.foreign_key end |
#association_primary_key(klass = nil) ⇒ Object
klass option is necessary to support loading polymorphic associations
484 485 486 |
# File 'activerecord/lib/active_record/reflection.rb', line 484 def association_primary_key(klass = nil) [:primary_key] || primary_key(klass || self.klass) end |
#association_primary_key_type ⇒ Object
488 489 490 |
# File 'activerecord/lib/active_record/reflection.rb', line 488 def association_primary_key_type klass.type_for_attribute(association_primary_key.to_s) end |
#association_scope_cache(conn, owner, &block) ⇒ Object
459 460 461 462 463 464 465 |
# File 'activerecord/lib/active_record/reflection.rb', line 459 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.
585 |
# File 'activerecord/lib/active_record/reflection.rb', line 585 def belongs_to?; false; end |
#check_preloadable! ⇒ Object Also known as: check_eager_loadable!
500 501 502 503 504 505 506 507 508 509 510 |
# File 'activerecord/lib/active_record/reflection.rb', line 500 def check_preloadable! return unless scope if 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
496 497 498 |
# File 'activerecord/lib/active_record/reflection.rb', line 496 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.
533 534 535 |
# File 'activerecord/lib/active_record/reflection.rb', line 533 def clear_association_scope_cache # :nodoc: @association_scope_cache.clear end |
#collect_join_chain ⇒ Object
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
527 528 529 |
# File 'activerecord/lib/active_record/reflection.rb', line 527 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.
567 568 569 |
# File 'activerecord/lib/active_record/reflection.rb', line 567 def collection? false end |
#compute_class(name) ⇒ Object
432 433 434 |
# File 'activerecord/lib/active_record/reflection.rb', line 432 def compute_class(name) active_record.send(:compute_type, name) end |
#constructable? ⇒ Boolean
:nodoc:
467 468 469 |
# File 'activerecord/lib/active_record/reflection.rb', line 467 def constructable? # :nodoc: @constructable end |
#extensions ⇒ Object
611 612 613 |
# File 'activerecord/lib/active_record/reflection.rb', line 611 def extensions Array([:extend]) end |
#foreign_key ⇒ Object
475 476 477 |
# File 'activerecord/lib/active_record/reflection.rb', line 475 def foreign_key @foreign_key ||= [:foreign_key] || derive_foreign_key.freeze end |
#has_inverse? ⇒ Boolean
545 546 547 |
# File 'activerecord/lib/active_record/reflection.rb', line 545 def has_inverse? inverse_name end |
#has_one? ⇒ Boolean
Returns true
if self
is a has_one
reflection.
588 |
# File 'activerecord/lib/active_record/reflection.rb', line 588 def has_one?; false; end |
#has_scope? ⇒ Boolean
541 542 543 |
# File 'activerecord/lib/active_record/reflection.rb', line 541 def has_scope? scope end |
#join_id_for(owner) ⇒ Object
:nodoc:
513 514 515 |
# File 'activerecord/lib/active_record/reflection.rb', line 513 def join_id_for(owner) # :nodoc: owner[join_foreign_key] end |
#join_table ⇒ Object
471 472 473 |
# File 'activerecord/lib/active_record/reflection.rb', line 471 def join_table @join_table ||= [:join_table] || derive_join_table end |
#klass ⇒ Object
Returns the target association’s class.
class Author < ActiveRecord::Base
has_many :books
end
Author.reflect_on_association(:books).klass
# => Book
Note: Do not call klass.new
or klass.create
to instantiate a new association object. Use build_association
or create_association
instead. This allows plugins to hook into association object creation.
428 429 430 |
# File 'activerecord/lib/active_record/reflection.rb', line 428 def klass @klass ||= compute_class(class_name) end |
#macro ⇒ Object
Returns the macro type.
has_many :clients
returns :has_many
562 |
# File 'activerecord/lib/active_record/reflection.rb', line 562 def macro; raise NotImplementedError; end |
#nested? ⇒ Boolean
537 538 539 |
# File 'activerecord/lib/active_record/reflection.rb', line 537 def nested? false end |
#polymorphic? ⇒ Boolean
592 593 594 |
# File 'activerecord/lib/active_record/reflection.rb', line 592 def polymorphic? [:polymorphic] end |
#polymorphic_inverse_of(associated_class) ⇒ Object
549 550 551 552 553 554 555 556 557 |
# File 'activerecord/lib/active_record/reflection.rb', line 549 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
521 522 523 |
# File 'activerecord/lib/active_record/reflection.rb', line 521 def source_reflection self end |
#through_reflection ⇒ Object
517 518 519 |
# File 'activerecord/lib/active_record/reflection.rb', line 517 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
580 581 582 |
# File 'activerecord/lib/active_record/reflection.rb', line 580 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || collection?) end |