Class: ActiveRecord::Reflection::ThroughReflection

Inherits:
AssociationReflection show all
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

#active_record, #macro, #name, #options

Instance Method Summary collapse

Methods inherited from AssociationReflection

#active_record_primary_key, #association_foreign_key, #association_primary_key, #belongs_to?, #build_association, #check_validity_of_inverse!, #collection?, #columns, #counter_cache_column, #create_association, #create_association!, #dependent_conditions, #has_inverse?, #initialize, #inverse_of, #klass, #polymorphic_inverse_of, #primary_key_column, #primary_key_name, #quoted_table_name, #reset_column_information, #table_name, #validate?

Methods inherited from MacroReflection

#==, #class_name, #initialize, #klass, #sanitized_conditions

Constructor Details

This class inherits a constructor from ActiveRecord::Reflection::AssociationReflection

Instance Method Details

#check_validity!Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/active_record/reflection.rb', line 372

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 options[:source_type] && source_reflection.options[:polymorphic].nil?
    raise HasManyThroughAssociationPointlessSourceTypeError.new(active_record.name, self, source_reflection)
  end

  if source_reflection.options[:polymorphic] && options[:source_type].nil?
    raise HasManyThroughAssociationPolymorphicError.new(active_record.name, self, source_reflection)
  end

  unless [:belongs_to, :has_many, :has_one].include?(source_reflection.macro) && source_reflection.options[:through].nil?
    raise HasManyThroughSourceAssociationMacroError.new(self)
  end

  check_validity_of_inverse!
end

#source_reflectionObject

Gets 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


345
346
347
# File 'lib/active_record/reflection.rb', line 345

def source_reflection
  @source_reflection ||= source_reflection_names.collect { |name| through_reflection.klass.reflect_on_association(name) }.compact.first
end

#source_reflection_namesObject

Gets an array of possible :through source reflection names:

[:singularized, :pluralized]


368
369
370
# File 'lib/active_record/reflection.rb', line 368

def source_reflection_names
  @source_reflection_names ||= (options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym }
end

#through_reflectionObject

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

tags_reflection = Post.reflect_on_association(:tags)
taggings_reflection = tags_reflection.through_reflection


360
361
362
# File 'lib/active_record/reflection.rb', line 360

def through_reflection
  @through_reflection ||= active_record.reflect_on_association(options[:through])
end

#through_reflection_primary_keyObject



396
397
398
# File 'lib/active_record/reflection.rb', line 396

def through_reflection_primary_key
  through_reflection.belongs_to? ? through_reflection.klass.primary_key : through_reflection.primary_key_name
end

#through_reflection_primary_key_nameObject



400
401
402
# File 'lib/active_record/reflection.rb', line 400

def through_reflection_primary_key_name
  through_reflection.primary_key_name if through_reflection.belongs_to?
end