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, #scope

Instance Method Summary collapse

Methods inherited from MacroReflection

#==, #class_name

Constructor Details

#initialize(*args) ⇒ AssociationReflection

Returns a new instance of AssociationReflection.



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

def initialize(*args)
  super
  @collection = [:has_many, :has_and_belongs_to_many].include?(macro)
end

Instance Method Details

#active_record_primary_keyObject



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

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

#association_classObject



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/active_record/reflection.rb', line 340

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



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

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



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

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)


332
333
334
# File 'lib/active_record/reflection.rb', line 332

def belongs_to?
  macro == :belongs_to
end

#build_association(attributes, &block) ⇒ Object

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



188
189
190
# File 'lib/active_record/reflection.rb', line 188

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

#chainObject

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



275
276
277
# File 'lib/active_record/reflection.rb', line 275

def chain
  [self]
end

#check_validity!Object



249
250
251
252
253
254
255
# File 'lib/active_record/reflection.rb', line 249

def check_validity!
  check_validity_of_inverse!

  if has_and_belongs_to_many? && association_foreign_key == foreign_key
    raise HasAndBelongsToManyAssociationForeignKeyNeeded.new(self)
  end
end

#check_validity_of_inverse!Object



257
258
259
260
261
262
263
# File 'lib/active_record/reflection.rb', line 257

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)


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

def collection?
  @collection
end

#columns(tbl_name) ⇒ Object



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

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

#counter_cache_columnObject



233
234
235
236
237
238
239
# File 'lib/active_record/reflection.rb', line 233

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



204
205
206
# File 'lib/active_record/reflection.rb', line 204

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

#foreign_typeObject



208
209
210
# File 'lib/active_record/reflection.rb', line 208

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

#has_and_belongs_to_many?Boolean

Returns:

  • (Boolean)


336
337
338
# File 'lib/active_record/reflection.rb', line 336

def has_and_belongs_to_many?
  macro == :has_and_belongs_to_many
end

#has_inverse?Boolean

Returns:

  • (Boolean)


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

def has_inverse?
  @options[:inverse_of]
end

#inverse_ofObject



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

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

#join_tableObject



200
201
202
# File 'lib/active_record/reflection.rb', line 200

def join_table
  @join_table ||= options[:join_table] || derive_join_table
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.



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

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

#nested?Boolean

Returns:

  • (Boolean)


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

def nested?
  false
end

#polymorphic?Boolean

Returns:

  • (Boolean)


365
366
367
# File 'lib/active_record/reflection.rb', line 365

def polymorphic?
  options.key? :polymorphic
end

#polymorphic_inverse_of(associated_class) ⇒ Object



301
302
303
304
305
306
307
308
309
# File 'lib/active_record/reflection.rb', line 301

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



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

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

#quoted_table_nameObject



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

def quoted_table_name
  @quoted_table_name ||= klass.quoted_table_name
end

#reset_column_informationObject



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

def reset_column_information
  @columns = nil
end

#scope_chainObject

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



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

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

#source_reflectionObject



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

def source_reflection
  nil
end

#table_nameObject



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

def table_name
  @table_name ||= klass.table_name
end

#through_reflectionObject



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

def through_reflection
  nil
end

#typeObject



212
213
214
# File 'lib/active_record/reflection.rb', line 212

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)


327
328
329
# File 'lib/active_record/reflection.rb', line 327

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