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
- #quoted_table_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
Returns the AssociationReflection object specified in the
:through
option of a HasManyThrough or HasOneThrough association.
Methods inherited from MacroReflection
#==, #class_name, #initialize, #macro, #name, #options, #sanitized_conditions
Constructor Details
This class inherits a constructor from ActiveRecord::Reflection::MacroReflection
Instance Method Details
#association_foreign_key ⇒ Object
148 149 150 |
# File 'lib/active_record/reflection.rb', line 148 def association_foreign_key @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key end |
#check_validity! ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/active_record/reflection.rb', line 196 def check_validity! if [:through] if through_reflection.nil? raise HasManyThroughAssociationNotFoundError.new(active_record.name, self) end if source_reflection.nil? raise HasManyThroughSourceAssociationNotFoundError.new(self) end if [:source_type] && source_reflection.[:polymorphic].nil? raise HasManyThroughAssociationPointlessSourceTypeError.new(active_record.name, self, source_reflection) end if source_reflection.[:polymorphic] && [:source_type].nil? raise HasManyThroughAssociationPolymorphicError.new(active_record.name, self, source_reflection) end unless [:belongs_to, :has_many].include?(source_reflection.macro) && source_reflection.[:through].nil? raise HasManyThroughSourceAssociationMacroError.new(self) end end end |
#counter_cache_column ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/active_record/reflection.rb', line 152 def counter_cache_column if [:counter_cache] == true "#{active_record.name.underscore.pluralize}_count" elsif [:counter_cache] [:counter_cache] end end |
#klass ⇒ Object
:nodoc:
132 133 134 |
# File 'lib/active_record/reflection.rb', line 132 def klass @klass ||= active_record.send(:compute_type, class_name) end |
#primary_key_name ⇒ Object
144 145 146 |
# File 'lib/active_record/reflection.rb', line 144 def primary_key_name @primary_key_name ||= [:foreign_key] || derive_primary_key_name end |
#quoted_table_name ⇒ Object
140 141 142 |
# File 'lib/active_record/reflection.rb', line 140 def quoted_table_name @quoted_table_name ||= klass.quoted_table_name 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 < ActiveRecord::Base
has_many :taggings
has_many :tags, :through => :taggings
end
191 192 193 194 |
# File 'lib/active_record/reflection.rb', line 191 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]
179 180 181 |
# File 'lib/active_record/reflection.rb', line 179 def source_reflection_names @source_reflection_names ||= ([:source] ? [[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym } end |
#table_name ⇒ Object
136 137 138 |
# File 'lib/active_record/reflection.rb', line 136 def table_name @table_name ||= klass.table_name end |
#through_reflection ⇒ Object
Returns the AssociationReflection object specified in the :through
option of a HasManyThrough or HasOneThrough association. Example:
class Post < ActiveRecord::Base
has_many :taggings
has_many :tags, :through => :taggings
end
= Post.reflect_on_association(:tags)
taggings_reflection = .through_reflection
171 172 173 |
# File 'lib/active_record/reflection.rb', line 171 def through_reflection @through_reflection ||= [:through] ? active_record.reflect_on_association([:through]) : false end |