Class: ActiveRecord::Reflection::AssociationReflection

Inherits:
MacroReflection show all
Defined in:
lib/active_record/reflection.rb

Overview

Holds all the meta-data about an 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 MacroReflection

#==, #class_name, #initialize, #macro, #name, #options

Constructor Details

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

Instance Method Details

#association_foreign_keyObject



144
145
146
# File 'lib/active_record/reflection.rb', line 144

def association_foreign_key
  @association_foreign_key ||= @options[:association_foreign_key] || class_name.foreign_key
end

#check_validity!Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/active_record/reflection.rb', line 180

def check_validity!
  if options[:through]
    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
end

#counter_cache_columnObject



148
149
150
151
152
153
154
# File 'lib/active_record/reflection.rb', line 148

def counter_cache_column
  if options[:counter_cache] == true
    "#{active_record.name.underscore.pluralize}_count"
  elsif options[:counter_cache]
    options[:counter_cache]
  end
end

#klassObject

:nodoc:



124
125
126
# File 'lib/active_record/reflection.rb', line 124

def klass
  @klass ||= active_record.send(:compute_type, class_name)
end

#primary_key_nameObject



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/active_record/reflection.rb', line 132

def primary_key_name
  return @primary_key_name if @primary_key_name
  case
    when macro == :belongs_to
      @primary_key_name = options[:foreign_key] || class_name.foreign_key
    when options[:as]
      @primary_key_name = options[:foreign_key] || "#{options[:as]}_id"
    else
      @primary_key_name = options[:foreign_key] || active_record.name.foreign_key
  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
  has_many :tags, :through => :taggings
end


175
176
177
178
# File 'lib/active_record/reflection.rb', line 175

def source_reflection
  return nil unless through_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]


164
165
166
# File 'lib/active_record/reflection.rb', line 164

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

#table_nameObject



128
129
130
# File 'lib/active_record/reflection.rb', line 128

def table_name
  @table_name ||= klass.table_name
end

#through_reflectionObject



156
157
158
# File 'lib/active_record/reflection.rb', line 156

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