Class: ActiveRecord::Reflection::AssociationReflection
Overview
Holds all the meta-data about an association as it was specified in the Active Record class.
Constant Summary
collapse
- VALID_AUTOMATIC_INVERSE_MACROS =
[:has_many, :has_one, :belongs_to]
- INVALID_AUTOMATIC_INVERSE_OPTIONS =
[:conditions, :through, :polymorphic, :foreign_key]
Instance Attribute Summary collapse
#active_record, #macro, #name, #options, #plural_name, #scope
Instance Method Summary
collapse
#==, #autosave=, #class_name
Constructor Details
#initialize(macro, name, scope, options, active_record) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
196
197
198
199
200
201
202
|
# File 'activerecord/lib/active_record/reflection.rb', line 196
def initialize(macro, name, scope, options, active_record)
super
@collection = [:has_many, :has_and_belongs_to_many].include?(macro)
@automatic_inverse_of = nil
@type = options[:as] && "#{options[:as]}_type"
@foreign_type = options[:foreign_type] || "#{name}_type"
end
|
Instance Attribute Details
#foreign_type ⇒ Object
Returns the value of attribute foreign_type
194
195
196
|
# File 'activerecord/lib/active_record/reflection.rb', line 194
def foreign_type
@foreign_type
end
|
Returns the value of attribute type
194
195
196
|
# File 'activerecord/lib/active_record/reflection.rb', line 194
def type
@type
end
|
Instance Method Details
#active_record_primary_key ⇒ Object
239
240
241
|
# File 'activerecord/lib/active_record/reflection.rb', line 239
def active_record_primary_key
@active_record_primary_key ||= options[:primary_key] || primary_key(active_record)
end
|
#association_foreign_key ⇒ Object
230
231
232
|
# File 'activerecord/lib/active_record/reflection.rb', line 230
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
235
236
237
|
# File 'activerecord/lib/active_record/reflection.rb', line 235
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.
334
335
336
|
# File 'activerecord/lib/active_record/reflection.rb', line 334
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.
206
207
208
|
# File 'activerecord/lib/active_record/reflection.rb', line 206
def build_association(attributes, &block)
klass.new(attributes, &block)
end
|
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
277
278
279
|
# File 'activerecord/lib/active_record/reflection.rb', line 277
def chain
[self]
end
|
#check_validity! ⇒ Object
251
252
253
254
255
256
257
|
# File 'activerecord/lib/active_record/reflection.rb', line 251
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
259
260
261
262
263
264
265
|
# File 'activerecord/lib/active_record/reflection.rb', line 259
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.
316
317
318
|
# File 'activerecord/lib/active_record/reflection.rb', line 316
def collection?
@collection
end
|
#counter_cache_column ⇒ Object
243
244
245
246
247
248
249
|
# File 'activerecord/lib/active_record/reflection.rb', line 243
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_key ⇒ Object
222
223
224
|
# File 'activerecord/lib/active_record/reflection.rb', line 222
def foreign_key
@foreign_key ||= options[:foreign_key] || derive_foreign_key
end
|
#has_and_belongs_to_many? ⇒ Boolean
338
339
340
|
# File 'activerecord/lib/active_record/reflection.rb', line 338
def has_and_belongs_to_many?
macro == :has_and_belongs_to_many
end
|
#has_inverse? ⇒ Boolean
293
294
295
|
# File 'activerecord/lib/active_record/reflection.rb', line 293
def has_inverse?
inverse_name
end
|
#inverse_of ⇒ Object
297
298
299
300
301
|
# File 'activerecord/lib/active_record/reflection.rb', line 297
def inverse_of
return unless inverse_name
@inverse_of ||= klass.reflect_on_association inverse_name
end
|
#join_table ⇒ Object
218
219
220
|
# File 'activerecord/lib/active_record/reflection.rb', line 218
def join_table
@join_table ||= options[:join_table] || derive_join_table
end
|
Returns the target association’s class.
class Author < ActiveRecord::Base
has_many :books
end
Author.reflect_on_association(:books).klass
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.
190
191
192
|
# File 'activerecord/lib/active_record/reflection.rb', line 190
def klass
@klass ||= active_record.send(:compute_type, class_name)
end
|
#nested? ⇒ Boolean
281
282
283
|
# File 'activerecord/lib/active_record/reflection.rb', line 281
def nested?
false
end
|
#polymorphic? ⇒ Boolean
367
368
369
|
# File 'activerecord/lib/active_record/reflection.rb', line 367
def polymorphic?
options.key? :polymorphic
end
|
#polymorphic_inverse_of(associated_class) ⇒ Object
303
304
305
306
307
308
309
310
311
|
# File 'activerecord/lib/active_record/reflection.rb', line 303
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_column ⇒ Object
226
227
228
|
# File 'activerecord/lib/active_record/reflection.rb', line 226
def primary_key_column
klass.columns_hash[klass.primary_key]
end
|
#quoted_table_name ⇒ Object
214
215
216
|
# File 'activerecord/lib/active_record/reflection.rb', line 214
def quoted_table_name
klass.quoted_table_name
end
|
#scope_chain ⇒ Object
An array of arrays of scopes. Each item in the outside array corresponds to a reflection in the #chain.
287
288
289
|
# File 'activerecord/lib/active_record/reflection.rb', line 287
def scope_chain
scope ? [[scope]] : [[]]
end
|
#source_reflection ⇒ Object
271
272
273
|
# File 'activerecord/lib/active_record/reflection.rb', line 271
def source_reflection
self
end
|
#table_name ⇒ Object
210
211
212
|
# File 'activerecord/lib/active_record/reflection.rb', line 210
def table_name
klass.table_name
end
|
#through_reflection ⇒ Object
267
268
269
|
# File 'activerecord/lib/active_record/reflection.rb', line 267
def through_reflection
nil
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
329
330
331
|
# File 'activerecord/lib/active_record/reflection.rb', line 329
def validate?
!options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
end
|