Class: ActiveRecord::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- MacroReflection
- ActiveRecord::Reflection::AssociationReflection
- 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
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
-
#foreign_type ⇒ Object
readonly
Returns the value of attribute foreign_type.
-
#parent_reflection ⇒ Object
[:name, Reflection].
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from MacroReflection
#active_record, #macro, #name, #options, #plural_name, #scope
Instance Method Summary collapse
- #active_record_primary_key ⇒ Object
- #association_class ⇒ Object
- #association_foreign_key ⇒ Object
-
#association_primary_key(klass = nil) ⇒ Object
klass option is necessary to support loading polymorphic associations.
-
#belongs_to? ⇒ Boolean
Returns
true
ifself
is abelongs_to
reflection. -
#build_association(attributes, &block) ⇒ Object
Returns a new, unsaved instance of the associated class.
-
#chain ⇒ Object
A chain of reflections from this one back to the owner.
- #check_validity! ⇒ Object
- #check_validity_of_inverse! ⇒ Object
-
#collection? ⇒ Boolean
Returns whether or not this association reflection is for a collection association.
-
#constructable? ⇒ Boolean
:nodoc:.
- #counter_cache_column ⇒ Object
- #foreign_key ⇒ Object
- #has_inverse? ⇒ Boolean
-
#initialize(macro, name, scope, options, active_record) ⇒ AssociationReflection
constructor
A new instance of AssociationReflection.
- #inverse_of ⇒ Object
- #join_table ⇒ Object
-
#klass ⇒ Object
Returns the target association’s class.
- #nested? ⇒ Boolean
- #polymorphic? ⇒ Boolean
- #polymorphic_inverse_of(associated_class) ⇒ Object
- #primary_key_column ⇒ Object
- #quoted_table_name ⇒ Object
-
#scope_chain ⇒ Object
An array of arrays of scopes.
- #source_reflection ⇒ Object
- #table_name ⇒ Object
- #through_reflection ⇒ Object
-
#validate? ⇒ Boolean
Returns whether or not the association should be validated as part of the parent’s validation.
Methods inherited from MacroReflection
Constructor Details
#initialize(macro, name, scope, options, active_record) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
227 228 229 230 231 232 233 234 |
# File 'activerecord/lib/active_record/reflection.rb', line 227 def initialize(macro, name, scope, , active_record) super @collection = [:has_many, :has_and_belongs_to_many].include?(macro) @automatic_inverse_of = nil @type = [:as] && "#{[:as]}_type" @foreign_type = [:foreign_type] || "#{name}_type" @constructable = calculate_constructable(macro, ) end |
Instance Attribute Details
#foreign_type ⇒ Object (readonly)
Returns the value of attribute foreign_type
224 225 226 |
# File 'activerecord/lib/active_record/reflection.rb', line 224 def foreign_type @foreign_type end |
#parent_reflection ⇒ Object
- :name, Reflection
225 226 227 |
# File 'activerecord/lib/active_record/reflection.rb', line 225 def parent_reflection @parent_reflection end |
#type ⇒ Object (readonly)
Returns the value of attribute type
224 225 226 |
# File 'activerecord/lib/active_record/reflection.rb', line 224 def type @type end |
Instance Method Details
#active_record_primary_key ⇒ Object
275 276 277 |
# File 'activerecord/lib/active_record/reflection.rb', line 275 def active_record_primary_key @active_record_primary_key ||= [:primary_key] || primary_key(active_record) end |
#association_class ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'activerecord/lib/active_record/reflection.rb', line 370 def association_class case macro when :belongs_to if [:polymorphic] Associations::BelongsToPolymorphicAssociation else Associations::BelongsToAssociation end when :has_many if [:through] Associations::HasManyThroughAssociation else Associations::HasManyAssociation end when :has_one if [:through] Associations::HasOneThroughAssociation else Associations::HasOneAssociation end end end |
#association_foreign_key ⇒ Object
266 267 268 |
# File 'activerecord/lib/active_record/reflection.rb', line 266 def association_foreign_key @association_foreign_key ||= [:association_foreign_key] || class_name.foreign_key end |
#association_primary_key(klass = nil) ⇒ Object
klass option is necessary to support loading polymorphic associations
271 272 273 |
# File 'activerecord/lib/active_record/reflection.rb', line 271 def association_primary_key(klass = nil) [:primary_key] || primary_key(klass || self.klass) end |
#belongs_to? ⇒ Boolean
Returns true
if self
is a belongs_to
reflection.
366 367 368 |
# File 'activerecord/lib/active_record/reflection.rb', line 366 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.
238 239 240 |
# File 'activerecord/lib/active_record/reflection.rb', line 238 def build_association(attributes, &block) klass.new(attributes, &block) end |
#chain ⇒ Object
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
309 310 311 |
# File 'activerecord/lib/active_record/reflection.rb', line 309 def chain [self] end |
#check_validity! ⇒ Object
287 288 289 |
# File 'activerecord/lib/active_record/reflection.rb', line 287 def check_validity! check_validity_of_inverse! end |
#check_validity_of_inverse! ⇒ Object
291 292 293 294 295 296 297 |
# File 'activerecord/lib/active_record/reflection.rb', line 291 def check_validity_of_inverse! unless [: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.
348 349 350 |
# File 'activerecord/lib/active_record/reflection.rb', line 348 def collection? @collection end |
#constructable? ⇒ Boolean
:nodoc:
242 243 244 |
# File 'activerecord/lib/active_record/reflection.rb', line 242 def constructable? # :nodoc: @constructable end |
#counter_cache_column ⇒ Object
279 280 281 282 283 284 285 |
# File 'activerecord/lib/active_record/reflection.rb', line 279 def counter_cache_column if [:counter_cache] == true "#{active_record.name.demodulize.underscore.pluralize}_count" elsif [:counter_cache] [:counter_cache].to_s end end |
#foreign_key ⇒ Object
258 259 260 |
# File 'activerecord/lib/active_record/reflection.rb', line 258 def foreign_key @foreign_key ||= [:foreign_key] || derive_foreign_key end |
#has_inverse? ⇒ Boolean
325 326 327 |
# File 'activerecord/lib/active_record/reflection.rb', line 325 def has_inverse? inverse_name end |
#inverse_of ⇒ Object
329 330 331 332 333 |
# File 'activerecord/lib/active_record/reflection.rb', line 329 def inverse_of return unless inverse_name @inverse_of ||= klass._reflect_on_association inverse_name end |
#join_table ⇒ Object
254 255 256 |
# File 'activerecord/lib/active_record/reflection.rb', line 254 def join_table @join_table ||= [:join_table] || derive_join_table end |
#klass ⇒ Object
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.
220 221 222 |
# File 'activerecord/lib/active_record/reflection.rb', line 220 def klass @klass ||= active_record.send(:compute_type, class_name) end |
#nested? ⇒ Boolean
313 314 315 |
# File 'activerecord/lib/active_record/reflection.rb', line 313 def nested? false end |
#polymorphic? ⇒ Boolean
393 394 395 |
# File 'activerecord/lib/active_record/reflection.rb', line 393 def polymorphic? .key? :polymorphic end |
#polymorphic_inverse_of(associated_class) ⇒ Object
335 336 337 338 339 340 341 342 343 |
# File 'activerecord/lib/active_record/reflection.rb', line 335 def polymorphic_inverse_of(associated_class) if has_inverse? if inverse_relationship = associated_class._reflect_on_association([:inverse_of]) inverse_relationship else raise InverseOfAssociationNotFoundError.new(self, associated_class) end end end |
#primary_key_column ⇒ Object
262 263 264 |
# File 'activerecord/lib/active_record/reflection.rb', line 262 def primary_key_column klass.columns_hash[klass.primary_key] end |
#quoted_table_name ⇒ Object
250 251 252 |
# File 'activerecord/lib/active_record/reflection.rb', line 250 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.
319 320 321 |
# File 'activerecord/lib/active_record/reflection.rb', line 319 def scope_chain scope ? [[scope]] : [[]] end |
#source_reflection ⇒ Object
303 304 305 |
# File 'activerecord/lib/active_record/reflection.rb', line 303 def source_reflection self end |
#table_name ⇒ Object
246 247 248 |
# File 'activerecord/lib/active_record/reflection.rb', line 246 def table_name klass.table_name end |
#through_reflection ⇒ Object
299 300 301 |
# File 'activerecord/lib/active_record/reflection.rb', line 299 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
361 362 363 |
# File 'activerecord/lib/active_record/reflection.rb', line 361 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || macro == :has_many) end |