Class: ActiveRecord::Reflection::ThroughReflection
- Inherits:
-
AbstractReflection
- Object
- AbstractReflection
- ActiveRecord::Reflection::ThroughReflection
- Defined in:
- lib/active_record/reflection.rb
Overview
Holds all the metadata about a :through association as it was specified in the Active Record class.
Instance Attribute Summary collapse
-
#delegate_reflection ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #add_as_polymorphic_through(reflection, seed) ⇒ Object
- #add_as_source(seed) ⇒ Object
- #add_as_through(seed) ⇒ Object
-
#association_primary_key(klass = nil) ⇒ Object
We want to use the klass from this reflection, rather than just delegate straight to the source_reflection, because the source_reflection may be polymorphic.
- #association_primary_key_type ⇒ Object
- #check_validity! ⇒ Object
-
#clear_association_scope_cache ⇒ Object
This is for clearing cache on the reflection.
-
#collect_join_chain ⇒ Object
Returns an array of reflections which are involved in this association.
- #collect_join_reflections(seed) ⇒ Object
- #constraints ⇒ Object
- #has_scope? ⇒ Boolean
-
#initialize(delegate_reflection) ⇒ ThroughReflection
constructor
A new instance of ThroughReflection.
-
#join_id_for(owner) ⇒ Object
:nodoc:.
-
#join_scopes(table, predicate_builder) ⇒ Object
:nodoc:.
- #klass ⇒ Object
-
#nested? ⇒ Boolean
A through association is nested if there would be more than one join table.
- #scopes ⇒ Object
- #source_options ⇒ Object
-
#source_reflection ⇒ Object
Returns the source of the through reflection.
-
#source_reflection_name ⇒ Object
:nodoc:.
-
#source_reflection_names ⇒ Object
Gets an array of possible
:through
source reflection names in both singular and plural form. - #source_type_scope ⇒ Object
- #through_options ⇒ Object
-
#through_reflection ⇒ Object
Returns the AssociationReflection object specified in the
:through
option of a HasManyThrough or HasOneThrough association. - #through_reflection? ⇒ Boolean
Methods inherited from AbstractReflection
#alias_candidate, #build_association, #chain, #check_validity_of_inverse!, #class_name, #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_keys, #klass_join_scope, #primary_key_type, #quoted_table_name, #scope_chain, #table_name
Constructor Details
#initialize(delegate_reflection) ⇒ ThroughReflection
Returns a new instance of ThroughReflection.
766 767 768 769 770 |
# File 'lib/active_record/reflection.rb', line 766 def initialize(delegate_reflection) @delegate_reflection = delegate_reflection @klass = delegate_reflection.[:anonymous_class] @source_reflection_name = delegate_reflection.[:source] end |
Instance Attribute Details
#delegate_reflection ⇒ Object (readonly)
:nodoc:
762 763 764 |
# File 'lib/active_record/reflection.rb', line 762 def delegate_reflection @delegate_reflection end |
Instance Method Details
#add_as_polymorphic_through(reflection, seed) ⇒ Object
980 981 982 |
# File 'lib/active_record/reflection.rb', line 980 def add_as_polymorphic_through(reflection, seed) collect_join_reflections(seed + [PolymorphicReflection.new(self, reflection)]) end |
#add_as_source(seed) ⇒ Object
976 977 978 |
# File 'lib/active_record/reflection.rb', line 976 def add_as_source(seed) collect_join_reflections seed end |
#add_as_through(seed) ⇒ Object
984 985 986 |
# File 'lib/active_record/reflection.rb', line 984 def add_as_through(seed) collect_join_reflections(seed + [self]) end |
#association_primary_key(klass = nil) ⇒ Object
We want to use the klass from this reflection, rather than just delegate straight to the source_reflection, because the source_reflection may be polymorphic. We still need to respect the source_reflection’s :primary_key option, though.
872 873 874 875 876 |
# File 'lib/active_record/reflection.rb', line 872 def association_primary_key(klass = nil) # Get the "actual" source reflection if the immediate source reflection has a # source reflection itself actual_source_reflection.[:primary_key] || primary_key(klass || self.klass) end |
#association_primary_key_type ⇒ Object
878 879 880 |
# File 'lib/active_record/reflection.rb', line 878 def association_primary_key_type klass.type_for_attribute(association_primary_key.to_s) end |
#check_validity! ⇒ Object
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 |
# File 'lib/active_record/reflection.rb', line 930 def check_validity! if through_reflection.nil? raise HasManyThroughAssociationNotFoundError.new(active_record.name, self) end if through_reflection.polymorphic? if has_one? raise HasOneAssociationPolymorphicThroughError.new(active_record.name, self) else raise HasManyThroughAssociationPolymorphicThroughError.new(active_record.name, self) end end if source_reflection.nil? raise HasManyThroughSourceAssociationNotFoundError.new(self) end if [:source_type] && !source_reflection.polymorphic? raise HasManyThroughAssociationPointlessSourceTypeError.new(active_record.name, self, source_reflection) end if source_reflection.polymorphic? && [:source_type].nil? raise HasManyThroughAssociationPolymorphicSourceError.new(active_record.name, self, source_reflection) end if has_one? && through_reflection.collection? raise HasOneThroughCantAssociateThroughCollection.new(active_record.name, self, through_reflection) end if parent_reflection.nil? reflections = active_record.reflections.keys.map(&:to_sym) if reflections.index(through_reflection.name) > reflections.index(name) raise HasManyThroughOrderError.new(active_record.name, self, through_reflection) end end 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.
840 841 842 843 844 |
# File 'lib/active_record/reflection.rb', line 840 def clear_association_scope_cache # :nodoc: delegate_reflection.clear_association_scope_cache source_reflection.clear_association_scope_cache through_reflection.clear_association_scope_cache end |
#collect_join_chain ⇒ Object
Returns an array of reflections which are involved in this association. Each item in the array corresponds to a table which will be part of the query for this association.
The chain is built by recursively calling #chain on the source reflection and the through reflection. The base case for the recursion is a normal association, which just returns
- self
-
as its #chain.
class Post < ActiveRecord::Base
has_many :taggings
has_many :tags, through: :taggings
end
tags_reflection = Post.reflect_on_association(:tags)
tags_reflection.chain
# => [<ActiveRecord::Reflection::ThroughReflection: @delegate_reflection=#<ActiveRecord::Reflection::HasManyReflection: @name=:tags...>,
<ActiveRecord::Reflection::HasManyReflection: @name=:taggings, @options={}, @active_record=Post>]
834 835 836 |
# File 'lib/active_record/reflection.rb', line 834 def collect_join_chain collect_join_reflections [self] end |
#collect_join_reflections(seed) ⇒ Object
988 989 990 991 992 993 994 995 |
# File 'lib/active_record/reflection.rb', line 988 def collect_join_reflections(seed) a = source_reflection.add_as_source seed if [:source_type] through_reflection.add_as_polymorphic_through self, a else through_reflection.add_as_through a end end |
#constraints ⇒ Object
970 971 972 973 974 |
# File 'lib/active_record/reflection.rb', line 970 def constraints scope_chain = source_reflection.constraints scope_chain << scope if scope scope_chain end |
#has_scope? ⇒ Boolean
858 859 860 861 862 |
# File 'lib/active_record/reflection.rb', line 858 def has_scope? scope || [:source_type] || source_reflection.has_scope? || through_reflection.has_scope? end |
#join_id_for(owner) ⇒ Object
:nodoc:
926 927 928 |
# File 'lib/active_record/reflection.rb', line 926 def join_id_for(owner) # :nodoc: source_reflection.join_id_for(owner) end |
#join_scopes(table, predicate_builder) ⇒ Object
:nodoc:
850 851 852 |
# File 'lib/active_record/reflection.rb', line 850 def join_scopes(table, predicate_builder) # :nodoc: source_reflection.join_scopes(table, predicate_builder) + super end |
#klass ⇒ Object
776 777 778 |
# File 'lib/active_record/reflection.rb', line 776 def klass @klass ||= delegate_reflection.compute_class(class_name) end |
#nested? ⇒ Boolean
A through association is nested if there would be more than one join table
865 866 867 |
# File 'lib/active_record/reflection.rb', line 865 def nested? source_reflection.through_reflection? || through_reflection.through_reflection? end |
#scopes ⇒ Object
846 847 848 |
# File 'lib/active_record/reflection.rb', line 846 def scopes source_reflection.scopes + super end |
#source_options ⇒ Object
918 919 920 |
# File 'lib/active_record/reflection.rb', line 918 def source_reflection. end |
#source_reflection ⇒ Object
Returns the source of the through reflection. It checks both a singularized and pluralized form for :belongs_to
or :has_many
.
class Post < ActiveRecord::Base
has_many :taggings
has_many :tags, through: :taggings
end
class Tagging < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
= Post.reflect_on_association(:tags)
.source_reflection
# => <ActiveRecord::Reflection::BelongsToReflection: @name=:tag, @active_record=Tagging, @plural_name="tags">
797 798 799 |
# File 'lib/active_record/reflection.rb', line 797 def source_reflection through_reflection.klass._reflect_on_association(source_reflection_name) end |
#source_reflection_name ⇒ Object
:nodoc:
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 |
# File 'lib/active_record/reflection.rb', line 897 def source_reflection_name # :nodoc: return @source_reflection_name if @source_reflection_name names = [name.to_s.singularize, name].collect(&:to_sym).uniq names = names.find_all { |n| through_reflection.klass._reflect_on_association(n) } if names.length > 1 raise AmbiguousSourceReflectionForThroughAssociation.new( active_record.name, macro, name, , source_reflection_names ) end @source_reflection_name = names.first end |
#source_reflection_names ⇒ Object
Gets an array of possible :through
source reflection names in both singular and plural form.
class Post < ActiveRecord::Base
has_many :taggings
has_many :tags, through: :taggings
end
= Post.reflect_on_association(:tags)
.source_reflection_names
# => [:tag, :tags]
893 894 895 |
# File 'lib/active_record/reflection.rb', line 893 def source_reflection_names [:source] ? [[:source]] : [name.to_s.singularize, name].uniq end |
#source_type_scope ⇒ Object
854 855 856 |
# File 'lib/active_record/reflection.rb', line 854 def source_type_scope through_reflection.klass.where(foreign_type => [:source_type]) end |
#through_options ⇒ Object
922 923 924 |
# File 'lib/active_record/reflection.rb', line 922 def through_reflection. end |
#through_reflection ⇒ Object
Returns the AssociationReflection object specified in the :through
option of a HasManyThrough or HasOneThrough association.
class Post < ActiveRecord::Base
has_many :taggings
has_many :tags, through: :taggings
end
= Post.reflect_on_association(:tags)
.through_reflection
# => <ActiveRecord::Reflection::HasManyReflection: @name=:taggings, @active_record=Post, @plural_name="taggings">
813 814 815 |
# File 'lib/active_record/reflection.rb', line 813 def through_reflection active_record._reflect_on_association([:through]) end |
#through_reflection? ⇒ Boolean
772 773 774 |
# File 'lib/active_record/reflection.rb', line 772 def through_reflection? true end |