Class: ActiveRecord::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- MacroReflection
- ActiveRecord::Reflection::AssociationReflection
- Defined in:
- lib/active_record/reflection.rb
Overview
Holds all the meta-data about an association as it was specified in the Active Record class.
Instance Attribute Summary
Attributes inherited from MacroReflection
Instance Method Summary collapse
- #association_foreign_key ⇒ Object
- #check_validity! ⇒ Object
- #counter_cache_column ⇒ Object
-
#klass ⇒ Object
:nodoc:.
- #primary_key_name ⇒ Object
-
#source_reflection ⇒ Object
Gets the source of the through reflection.
-
#source_reflection_names ⇒ Object
Gets an array of possible :through source reflection names.
- #table_name ⇒ Object
- #through_reflection ⇒ Object
Methods inherited from MacroReflection
#==, #class_name, #initialize, #macro, #name, #options
Constructor Details
This class inherits a constructor from ActiveRecord::Reflection::MacroReflection
Instance Method Details
#association_foreign_key ⇒ Object
131 132 133 |
# File 'lib/active_record/reflection.rb', line 131 def association_foreign_key @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key end |
#check_validity! ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/active_record/reflection.rb', line 166 def check_validity! if [:through] if through_reflection.nil? raise HasManyThroughAssociationNotFoundError.new(self) end if source_reflection.nil? raise HasManyThroughSourceAssociationNotFoundError.new(self) end if source_reflection.[:polymorphic] raise HasManyThroughAssociationPolymorphicError.new(class_name, @reflection, source_reflection) end end end |
#counter_cache_column ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/active_record/reflection.rb', line 135 def counter_cache_column if [:counter_cache] == true "#{active_record.name.underscore.pluralize}_count" elsif [:counter_cache] [:counter_cache] end end |
#klass ⇒ Object
:nodoc:
111 112 113 |
# File 'lib/active_record/reflection.rb', line 111 def klass @klass ||= active_record.send(:compute_type, class_name) end |
#primary_key_name ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/active_record/reflection.rb', line 119 def primary_key_name return @primary_key_name if @primary_key_name case when macro == :belongs_to @primary_key_name = [:foreign_key] || class_name.foreign_key when [:as] @primary_key_name = [:foreign_key] || "#{[:as]}_id" else @primary_key_name = [:foreign_key] || active_record.name.foreign_key end end |
#source_reflection ⇒ Object
Gets the source of the through reflection. It checks both a singularized and pluralized form for :belongs_to or :has_many. (The :tags association on Tagging below)
class Post
has_many :tags, :through => :taggings
end
161 162 163 164 |
# File 'lib/active_record/reflection.rb', line 161 def source_reflection return nil unless through_reflection @source_reflection ||= source_reflection_names.collect { |name| through_reflection.klass.reflect_on_association(name) }.compact.first end |
#source_reflection_names ⇒ Object
Gets an array of possible :through source reflection names
[singularized, pluralized]
150 151 152 |
# File 'lib/active_record/reflection.rb', line 150 def source_reflection_names @source_reflection_names ||= ([:source] ? [[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym } end |
#table_name ⇒ Object
115 116 117 |
# File 'lib/active_record/reflection.rb', line 115 def table_name @table_name ||= klass.table_name end |
#through_reflection ⇒ Object
143 144 145 |
# File 'lib/active_record/reflection.rb', line 143 def through_reflection @through_reflection ||= [:through] ? active_record.reflect_on_association([:through]) : false end |