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, #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 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].include?(source_reflection.macro) && source_reflection.options[:through].nil?
    raise HasManyThroughSourceAssociationMacroError.new(self)
  end
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


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_namesObject

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 ||= (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


266
267
268
# File 'lib/active_record/reflection.rb', line 266

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

#through_reflection_primary_keyObject



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_nameObject



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