Class: ActiveRecord::Reflection::AssociationReflection

Inherits:
MacroReflection show all
Defined in:
activerecord/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

Constant Summary

VALID_AUTOMATIC_INVERSE_MACROS =
[:has_many, :has_one, :belongs_to]
INVALID_AUTOMATIC_INVERSE_OPTIONS =
[:conditions, :through, :polymorphic, :foreign_key]

Instance Attribute Summary (collapse)

Attributes inherited from MacroReflection

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

Instance Method Summary (collapse)

Methods inherited from MacroReflection

#==, #autosave=, #class_name

Constructor Details

- (AssociationReflection) initialize(macro, name, scope, options, active_record)

Returns a new instance of AssociationReflection



197
198
199
200
201
202
203
204
205
206
# File 'activerecord/lib/active_record/reflection.rb', line 197

def initialize(macro, name, scope, options, active_record)
  super
  @collection = :has_many == macro
  @automatic_inverse_of = nil
  @type         = options[:as] && "#{options[:as]}_type"
  @foreign_type = options[:foreign_type] || "#{name}_type"
  @constructable = calculate_constructable(macro, options)
  @association_scope_cache = {}
  @scope_lock = Mutex.new
end

Instance Attribute Details

- (Object) foreign_type (readonly)

Returns the value of attribute foreign_type



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

def foreign_type
  @foreign_type
end

- (Object) type (readonly)

Returns the value of attribute type



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

def type
  @type
end

Instance Method Details

- (Object) active_record_primary_key



257
258
259
# File 'activerecord/lib/active_record/reflection.rb', line 257

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

- (Object) association_class



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'activerecord/lib/active_record/reflection.rb', line 352

def association_class
  case macro
  when :belongs_to
    if options[:polymorphic]
      Associations::BelongsToPolymorphicAssociation
    else
      Associations::BelongsToAssociation
    end
  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

- (Object) association_foreign_key



248
249
250
# File 'activerecord/lib/active_record/reflection.rb', line 248

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

- (Object) association_primary_key(klass = nil)

klass option is necessary to support loading polymorphic associations



253
254
255
# File 'activerecord/lib/active_record/reflection.rb', line 253

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

- (Object) association_scope_cache(conn, owner)



208
209
210
211
212
213
214
215
216
# File 'activerecord/lib/active_record/reflection.rb', line 208

def association_scope_cache(conn, owner)
  key = conn.prepared_statements
  if options[:polymorphic]
    key = [key, owner.read_attribute(@foreign_type)]
  end
  @association_scope_cache[key] ||= @scope_lock.synchronize {
    @association_scope_cache[key] ||= yield
  }
end

- (Boolean) belongs_to?

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


348
349
350
# File 'activerecord/lib/active_record/reflection.rb', line 348

def belongs_to?
  macro == :belongs_to
end

- (Object) build_association(attributes, &block)

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



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

def build_association(attributes, &block)
  klass.new(attributes, &block)
end

- (Object) chain

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



291
292
293
# File 'activerecord/lib/active_record/reflection.rb', line 291

def chain
  [self]
end

- (Object) check_validity!



269
270
271
# File 'activerecord/lib/active_record/reflection.rb', line 269

def check_validity!
  check_validity_of_inverse!
end

- (Object) check_validity_of_inverse!



273
274
275
276
277
278
279
# File 'activerecord/lib/active_record/reflection.rb', line 273

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

- (Boolean) collection?

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)


330
331
332
# File 'activerecord/lib/active_record/reflection.rb', line 330

def collection?
  @collection
end

- (Boolean) constructable?

:nodoc:

Returns:

  • (Boolean)


224
225
226
# File 'activerecord/lib/active_record/reflection.rb', line 224

def constructable? # :nodoc:
  @constructable
end

- (Object) counter_cache_column



261
262
263
264
265
266
267
# File 'activerecord/lib/active_record/reflection.rb', line 261

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

- (Object) foreign_key



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

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

- (Boolean) has_inverse?

Returns:

  • (Boolean)


307
308
309
# File 'activerecord/lib/active_record/reflection.rb', line 307

def has_inverse?
  inverse_name
end

- (Object) inverse_of



311
312
313
314
315
# File 'activerecord/lib/active_record/reflection.rb', line 311

def inverse_of
  return unless inverse_name

  @inverse_of ||= klass.reflect_on_association inverse_name
end

- (Object) join_table



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

def join_table
  @join_table ||= options[:join_table] || derive_join_table
end

- (Object) klass

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.



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

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

- (Boolean) nested?

Returns:

  • (Boolean)


295
296
297
# File 'activerecord/lib/active_record/reflection.rb', line 295

def nested?
  false
end

- (Boolean) polymorphic?

Returns:

  • (Boolean)


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

def polymorphic?
  options.key? :polymorphic
end

- (Object) polymorphic_inverse_of(associated_class)



317
318
319
320
321
322
323
324
325
# File 'activerecord/lib/active_record/reflection.rb', line 317

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

- (Object) primary_key_column



244
245
246
# File 'activerecord/lib/active_record/reflection.rb', line 244

def primary_key_column
  klass.columns_hash[klass.primary_key]
end

- (Object) quoted_table_name



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

def quoted_table_name
  klass.quoted_table_name
end

- (Object) scope_chain

An array of arrays of scopes. Each item in the outside array corresponds to a reflection in the #chain.



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

def scope_chain
  scope ? [[scope]] : [[]]
end

- (Object) source_reflection



285
286
287
# File 'activerecord/lib/active_record/reflection.rb', line 285

def source_reflection
  self
end

- (Object) table_name



228
229
230
# File 'activerecord/lib/active_record/reflection.rb', line 228

def table_name
  klass.table_name
end

- (Object) through_reflection



281
282
283
# File 'activerecord/lib/active_record/reflection.rb', line 281

def through_reflection
  nil
end

- (Boolean) validate?

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)


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

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