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 (collapse)
#active_record, #macro, #name, #options, #plural_name
Instance Method Summary
(collapse)
#==, #class_name, #sanitized_conditions
Constructor Details
- (AssociationReflection) initialize(macro, name, options, active_record)
A new instance of AssociationReflection
176
177
178
179
|
# File 'activerecord/lib/active_record/reflection.rb', line 176
def initialize(macro, name, options, active_record)
super
@collection = macro.in?([:has_many, :has_and_belongs_to_many])
end
|
Instance Attribute Details
- (Object) original_build_association_called
This is a hack so that we can tell if build_association was overridden, in
order to provide an appropriate deprecation if the overridden method
ignored the &block. Please see Association#build_record for details.
184
185
186
|
# File 'activerecord/lib/active_record/reflection.rb', line 184
def original_build_association_called
@original_build_association_called
end
|
Instance Method Details
- (Object) active_record_primary_key
231
232
233
|
# File 'activerecord/lib/active_record/reflection.rb', line 231
def active_record_primary_key
@active_record_primary_key ||= options[:primary_key] || primary_key(active_record)
end
|
- (Object) association_class
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
# File 'activerecord/lib/active_record/reflection.rb', line 331
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
|
- (Object) association_foreign_key
222
223
224
|
# File 'activerecord/lib/active_record/reflection.rb', line 222
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
227
228
229
|
# File 'activerecord/lib/active_record/reflection.rb', line 227
def association_primary_key(klass = nil)
options[:primary_key] || primary_key(klass || self.klass)
end
|
- (Boolean) belongs_to?
Returns true if self is a belongs_to reflection.
327
328
329
|
# File 'activerecord/lib/active_record/reflection.rb', line 327
def belongs_to?
macro == :belongs_to
end
|
- (Object) build_association(*options, &block)
Returns a new, unsaved instance of the associated class. options
will be passed to the class's constructor.
188
189
190
191
|
# File 'activerecord/lib/active_record/reflection.rb', line 188
def build_association(*options, &block)
@original_build_association_called = true
klass.new(*options, &block)
end
|
A chain of reflections from this one back to the owner. For more see the
explanation in ThroughReflection.
273
274
275
|
# File 'activerecord/lib/active_record/reflection.rb', line 273
def chain
[self]
end
|
- (Object) check_validity!
251
252
253
|
# File 'activerecord/lib/active_record/reflection.rb', line 251
def check_validity!
check_validity_of_inverse!
end
|
- (Object) check_validity_of_inverse!
255
256
257
258
259
260
261
|
# File 'activerecord/lib/active_record/reflection.rb', line 255
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.
309
310
311
|
# File 'activerecord/lib/active_record/reflection.rb', line 309
def collection?
@collection
end
|
- (Object) columns(tbl_name, log_msg)
243
244
245
|
# File 'activerecord/lib/active_record/reflection.rb', line 243
def columns(tbl_name, log_msg)
@columns ||= klass.connection.columns(tbl_name, log_msg)
end
|
- (Object) conditions
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...)
280
281
282
|
# File 'activerecord/lib/active_record/reflection.rb', line 280
def conditions
[[options[:conditions]].compact]
end
|
- (Object) counter_cache_column
235
236
237
238
239
240
241
|
# File 'activerecord/lib/active_record/reflection.rb', line 235
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
201
202
203
|
# File 'activerecord/lib/active_record/reflection.rb', line 201
def foreign_key
@foreign_key ||= options[:foreign_key] || derive_foreign_key
end
|
- (Object) foreign_type
210
211
212
|
# File 'activerecord/lib/active_record/reflection.rb', line 210
def foreign_type
@foreign_type ||= options[:foreign_type] || "#{name}_type"
end
|
- (Boolean) has_inverse?
286
287
288
|
# File 'activerecord/lib/active_record/reflection.rb', line 286
def has_inverse?
@options[:inverse_of]
end
|
- (Object) inverse_of
290
291
292
293
294
|
# File 'activerecord/lib/active_record/reflection.rb', line 290
def inverse_of
if has_inverse?
@inverse_of ||= klass.reflect_on_association(options[:inverse_of])
end
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.
172
173
174
|
# File 'activerecord/lib/active_record/reflection.rb', line 172
def klass
@klass ||= active_record.send(:compute_type, class_name)
end
|
- (Object) polymorphic_inverse_of(associated_class)
296
297
298
299
300
301
302
303
304
|
# File 'activerecord/lib/active_record/reflection.rb', line 296
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
218
219
220
|
# File 'activerecord/lib/active_record/reflection.rb', line 218
def primary_key_column
@primary_key_column ||= klass.columns.find { |c| c.name == klass.primary_key }
end
|
- (Object) primary_key_name
205
206
207
|
# File 'activerecord/lib/active_record/reflection.rb', line 205
def primary_key_name
foreign_key
end
|
- (Object) quoted_table_name
197
198
199
|
# File 'activerecord/lib/active_record/reflection.rb', line 197
def quoted_table_name
@quoted_table_name ||= klass.quoted_table_name
end
|
247
248
249
|
# File 'activerecord/lib/active_record/reflection.rb', line 247
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
193
194
195
|
# File 'activerecord/lib/active_record/reflection.rb', line 193
def table_name
@table_name ||= klass.table_name
end
|
- (Object) through_reflection
263
264
265
|
# File 'activerecord/lib/active_record/reflection.rb', line 263
def through_reflection
nil
end
|
214
215
216
|
# File 'activerecord/lib/active_record/reflection.rb', line 214
def type
@type ||= options[:as] && "#{options[:as]}_type"
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
322
323
324
|
# File 'activerecord/lib/active_record/reflection.rb', line 322
def validate?
!options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
end
|