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

Instance Method Summary collapse

Methods inherited from AssociationReflection

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

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



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/active_record/reflection.rb', line 346

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. (The :tags association on Tagging below.)

class Post < ActiveRecord::Base
  has_many :taggings
  has_many :tags, :through => :taggings
end


319
320
321
# File 'lib/active_record/reflection.rb', line 319

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]


342
343
344
# File 'lib/active_record/reflection.rb', line 342

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. Example:

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


334
335
336
# File 'lib/active_record/reflection.rb', line 334

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

#through_reflection_primary_keyObject



370
371
372
# File 'lib/active_record/reflection.rb', line 370

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



374
375
376
# File 'lib/active_record/reflection.rb', line 374

def through_reflection_primary_key_name
  through_reflection.primary_key_name if through_reflection.belongs_to?
end