Class: ActiveRecord::Reflection::ThroughReflection
- Inherits:
-
AssociationReflection
- Object
- MacroReflection
- AssociationReflection
- ActiveRecord::Reflection::ThroughReflection
- Defined in:
- lib/active_record/reflection.rb
Overview
Holds all the meta-data about a :through association as it was specified in the Active Record class.
Instance Attribute Summary
Attributes inherited from MacroReflection
Instance Method Summary collapse
- #check_validity! ⇒ Object
-
#source_reflection ⇒ Object
Gets the source of the through reflection.
-
#source_reflection_names ⇒ Object
Gets an array of possible
:through
source reflection names:. -
#through_reflection ⇒ Object
Returns the AssociationReflection object specified in the
:through
option of a HasManyThrough or HasOneThrough association. - #through_reflection_primary_key ⇒ Object
- #through_reflection_primary_key_name ⇒ Object
Methods inherited from AssociationReflection
#association_foreign_key, #build_association, #columns, #counter_cache_column, #create_association, #create_association!, #klass, #primary_key_name, #quoted_table_name, #reset_column_information, #table_name
Methods inherited from MacroReflection
#==, #belongs_to?, #class_name, #initialize, #klass, #macro, #name, #options, #sanitized_conditions
Constructor Details
This class inherits a constructor from ActiveRecord::Reflection::MacroReflection
Instance Method Details
#check_validity! ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/active_record/reflection.rb', line 278 def check_validity! 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 |
#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
251 252 253 |
# File 'lib/active_record/reflection.rb', line 251 def source_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]
274 275 276 |
# File 'lib/active_record/reflection.rb', line 274 def source_reflection_names @source_reflection_names ||= ([:source] ? [[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym } 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
266 267 268 |
# File 'lib/active_record/reflection.rb', line 266 def through_reflection @through_reflection ||= active_record.reflect_on_association([:through]) end |
#through_reflection_primary_key ⇒ Object
300 301 302 |
# File 'lib/active_record/reflection.rb', line 300 def through_reflection_primary_key through_reflection.belongs_to? ? through_reflection.klass.primary_key : through_reflection.primary_key_name end |
#through_reflection_primary_key_name ⇒ Object
304 305 306 |
# File 'lib/active_record/reflection.rb', line 304 def through_reflection_primary_key_name through_reflection.primary_key_name if through_reflection.belongs_to? end |