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.

Direct Known Subclasses

ThroughReflection

Instance Attribute Summary

Attributes inherited from MacroReflection

#active_record, #macro, #name, #options, #plural_name

Instance Method Summary collapse

Methods inherited from MacroReflection

#==, #class_name, #sanitized_conditions

Constructor Details

#initialize(macro, name, options, active_record) ⇒ AssociationReflection

Returns a new instance of AssociationReflection.



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

def initialize(macro, name, options, active_record)
  super
  @collection = macro.in?([:has_many, :has_and_belongs_to_many])
end

Instance Method Details

#active_record_primary_keyObject



219
220
221
# File 'lib/active_record/reflection.rb', line 219

def active_record_primary_key
  @active_record_primary_key ||= options[:primary_key] || primary_key(active_record)
end

#association_classObject



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/active_record/reflection.rb', line 323

def association_class
  case macro
  when :belongs_to
    if options[:polymorphic]
      Associations::BelongsToPolymorphicAssociation
    else
      Associations::BelongsToAssociation
    end
  when :has_and_belongs_to_many
    Associations::HasAndBelongsToManyAssociation
  when :has_many
    if options[:through]
      Associations::HasManyThroughAssociation
    else
      Associations::HasManyAssociation
    end
  when :has_one
    if options[:through]
      Associations::HasOneThroughAssociation
    else
      Associations::HasOneAssociation
    end
  end
end

#association_foreign_keyObject



210
211
212
# File 'lib/active_record/reflection.rb', line 210

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

#association_primary_key(klass = nil) ⇒ Object

klass option is necessary to support loading polymorphic associations



215
216
217
# File 'lib/active_record/reflection.rb', line 215

def association_primary_key(klass = nil)
  options[:primary_key] || primary_key(klass || self.klass)
end

#belongs_to?Boolean

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


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

def belongs_to?
  macro == :belongs_to
end

#build_association(*options, &block) ⇒ Object

Returns a new, unsaved instance of the associated class. options will be passed to the class’s constructor.



182
183
184
# File 'lib/active_record/reflection.rb', line 182

def build_association(*options, &block)
  klass.new(*options, &block)
end

#chainObject

A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.



261
262
263
# File 'lib/active_record/reflection.rb', line 261

def chain
  [self]
end

#check_validity!Object



239
240
241
# File 'lib/active_record/reflection.rb', line 239

def check_validity!
  check_validity_of_inverse!
end

#check_validity_of_inverse!Object



243
244
245
246
247
248
249
# File 'lib/active_record/reflection.rb', line 243

def check_validity_of_inverse!
  unless options[:polymorphic]
    if has_inverse? && inverse_of.nil?
      raise InverseOfAssociationNotFoundError.new(self)
    end
  end
end

#collection?Boolean

Returns whether or not this association reflection is for a collection association. Returns true if the macro is either has_many or has_and_belongs_to_many, false otherwise.

Returns:

  • (Boolean)


301
302
303
# File 'lib/active_record/reflection.rb', line 301

def collection?
  @collection
end

#columns(tbl_name, log_msg) ⇒ Object



231
232
233
# File 'lib/active_record/reflection.rb', line 231

def columns(tbl_name, log_msg)
  @columns ||= klass.connection.columns(tbl_name, log_msg)
end

#conditionsObject

An array of arrays of conditions. Each item in the outside array corresponds to a reflection in the #chain. The inside arrays are simply conditions (and each condition may itself be a hash, array, arel predicate, etc…)



272
273
274
# File 'lib/active_record/reflection.rb', line 272

def conditions
  [[options[:conditions]].compact]
end

#counter_cache_columnObject



223
224
225
226
227
228
229
# File 'lib/active_record/reflection.rb', line 223

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

#foreign_keyObject



194
195
196
# File 'lib/active_record/reflection.rb', line 194

def foreign_key
  @foreign_key ||= options[:foreign_key] || derive_foreign_key
end

#foreign_typeObject



198
199
200
# File 'lib/active_record/reflection.rb', line 198

def foreign_type
  @foreign_type ||= options[:foreign_type] || "#{name}_type"
end

#has_inverse?Boolean

Returns:

  • (Boolean)


278
279
280
# File 'lib/active_record/reflection.rb', line 278

def has_inverse?
  @options[:inverse_of]
end

#inverse_ofObject



282
283
284
285
286
# File 'lib/active_record/reflection.rb', line 282

def inverse_of
  if has_inverse?
    @inverse_of ||= klass.reflect_on_association(options[:inverse_of])
  end
end

#klassObject

Returns the target association’s class.

class Author < ActiveRecord::Base
  has_many :books
end

Author.reflect_on_association(:books).klass
# => Book

Note: Do not call klass.new or klass.create to instantiate a new association object. Use build_association or create_association instead. This allows plugins to hook into association object creation.



171
172
173
# File 'lib/active_record/reflection.rb', line 171

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

#nested?Boolean

Returns:

  • (Boolean)


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

def nested?
  false
end

#polymorphic_inverse_of(associated_class) ⇒ Object



288
289
290
291
292
293
294
295
296
# File 'lib/active_record/reflection.rb', line 288

def polymorphic_inverse_of(associated_class)
  if has_inverse?
    if inverse_relationship = associated_class.reflect_on_association(options[:inverse_of])
      inverse_relationship
    else
      raise InverseOfAssociationNotFoundError.new(self, associated_class)
    end
  end
end

#primary_key_columnObject



206
207
208
# File 'lib/active_record/reflection.rb', line 206

def primary_key_column
  @primary_key_column ||= klass.columns.find { |c| c.name == klass.primary_key }
end

#quoted_table_nameObject



190
191
192
# File 'lib/active_record/reflection.rb', line 190

def quoted_table_name
  @quoted_table_name ||= klass.quoted_table_name
end

#reset_column_informationObject



235
236
237
# File 'lib/active_record/reflection.rb', line 235

def reset_column_information
  @columns = nil
end

#source_reflectionObject



255
256
257
# File 'lib/active_record/reflection.rb', line 255

def source_reflection
  nil
end

#table_nameObject



186
187
188
# File 'lib/active_record/reflection.rb', line 186

def table_name
  @table_name ||= klass.table_name
end

#through_reflectionObject



251
252
253
# File 'lib/active_record/reflection.rb', line 251

def through_reflection
  nil
end

#typeObject



202
203
204
# File 'lib/active_record/reflection.rb', line 202

def type
  @type ||= options[:as] && "#{options[:as]}_type"
end

#validate?Boolean

Returns whether or not the association should be validated as part of the parent’s validation.

Unless you explicitly disable validation with :validate => false, validation will take place when:

  • you explicitly enable validation; :validate => true

  • you use autosave; :autosave => true

  • the association is a has_many association

Returns:

  • (Boolean)


314
315
316
# File 'lib/active_record/reflection.rb', line 314

def validate?
  !options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
end