Class: ActiveRecord::Reflection::ThroughReflection
- Inherits:
-
AssociationReflection
- Object
- MacroReflection
- AssociationReflection
- ActiveRecord::Reflection::ThroughReflection
- Defined in:
- lib/gems/activerecord-2.2.2/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, #counter_cache_column, #create_association, #create_association!, #klass, #primary_key_name, #quoted_table_name, #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
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 270 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
243 244 245 |
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 243 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]
266 267 268 |
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 266 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
258 259 260 |
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 258 def through_reflection @through_reflection ||= active_record.reflect_on_association([:through]) end |
#through_reflection_primary_key ⇒ Object
292 293 294 |
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 292 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
296 297 298 |
# File 'lib/gems/activerecord-2.2.2/lib/active_record/reflection.rb', line 296 def through_reflection_primary_key_name through_reflection.primary_key_name if through_reflection.belongs_to? end |