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.

Instance Attribute Summary

Attributes inherited from MacroReflection

#active_record, #macro, #name, #options

Instance Method Summary (collapse)

Methods inherited from MacroReflection

#==, #class_name, #sanitized_conditions

Constructor Details

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

A new instance of AssociationReflection



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

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

Instance Method Details

- (Object) active_record_primary_key



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

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

- (Object) association_foreign_key



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

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

- (Object) association_primary_key



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

def association_primary_key
  @association_primary_key ||=
    options[:primary_key] ||
    !options[:polymorphic] && klass.primary_key ||
    'id'
end

- (Boolean) belongs_to?

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


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

def belongs_to?
  macro == :belongs_to
end

- (Object) build_association(*options)

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



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

def build_association(*options)
  klass.new(*options)
end

- (Object) check_validity!



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

def check_validity!
  check_validity_of_inverse!
end

- (Object) check_validity_of_inverse!



252
253
254
255
256
257
258
# File 'activerecord/lib/active_record/reflection.rb', line 252

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)


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

def collection?
  @collection
end

- (Object) columns(tbl_name, log_msg)



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

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

- (Object) counter_cache_column



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

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

- (Object) create_association(*options)

Creates a new instance of the associated class, and immediately saves it with ActiveRecord::Base#save. options will be passed to the class's creation method. Returns the newly created object.



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

def create_association(*options)
  klass.create(*options)
end

- (Object) create_association!(*options)

Creates a new instance of the associated class, and immediately saves it with ActiveRecord::Base#save!. options will be passed to the class's creation method. If the created record doesn't pass validations, then an exception will be raised.

Returns the newly created object.



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

def create_association!(*options)
  klass.create!(*options)
end

- (Object) foreign_key



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

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

- (Object) foreign_type



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

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

- (Boolean) has_inverse?

Returns:

  • (Boolean)


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

def has_inverse?
  @options[:inverse_of]
end

- (Object) inverse_of



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

def inverse_of
  if has_inverse?
    @inverse_of ||= klass.reflect_on_association(options[:inverse_of])
  end
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.



160
161
162
# File 'activerecord/lib/active_record/reflection.rb', line 160

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

- (Object) polymorphic_inverse_of(associated_class)



281
282
283
284
285
286
287
288
289
# File 'activerecord/lib/active_record/reflection.rb', line 281

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



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

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

- (Object) primary_key_name



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

def primary_key_name
  foreign_key
end

- (Object) proxy_class



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'activerecord/lib/active_record/reflection.rb', line 316

def proxy_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

- (Object) quoted_table_name



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

def quoted_table_name
  @quoted_table_name ||= klass.quoted_table_name
end

- (Object) reset_column_information



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

def reset_column_information
  @columns = nil
end

- (Object) source_reflection



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

def source_reflection
  nil
end

- (Object) table_name



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

def table_name
  @table_name ||= klass.table_name
end

- (Object) through_reflection



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

def through_reflection
  false
end

- (Object) through_reflection_foreign_key



264
265
# File 'activerecord/lib/active_record/reflection.rb', line 264

def through_reflection_foreign_key
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)


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

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