Class: ActiveRecord::Associations::HasManyAssociation

Inherits:
CollectionAssociation show all
Includes:
ForeignAssociation
Defined in:
lib/active_record/associations/has_many_association.rb

Overview

This is the proxy that handles a has many association.

If the association has a :through option further specialization is provided by its child HasManyThroughAssociation.

Direct Known Subclasses

HasManyThroughAssociation

Instance Attribute Summary

Attributes inherited from Association

#inversed, #owner, #reflection, #target

Instance Method Summary collapse

Methods included from ForeignAssociation

#foreign_key_present?

Methods inherited from CollectionAssociation

#add_to_target, #any?, #build, #concat, #count, #create, #create!, #delete, #delete_all, #destroy, #destroy_all, #distinct, #fifth, #find, #first, #forty_two, #fourth, #ids_reader, #ids_writer, #include?, #last, #length, #load_target, #many?, #null_scope?, #reader, #replace, #reset, #scope, #second, #second_to_last, #select, #size, #take, #third, #third_to_last, #transaction, #writer

Methods inherited from Association

#aliased_table_name, #association_scope, #extensions, #initialize, #initialize_attributes, #interpolate, #klass, #load_target, #loaded!, #loaded?, #marshal_dump, #marshal_load, #reload, #reset, #reset_scope, #scope, #set_inverse_instance, #stale_target?, #target_scope

Constructor Details

This class inherits a constructor from ActiveRecord::Associations::Association

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/active_record/associations/has_many_association.rb', line 46

def empty?
  if reflection.has_cached_counter?
    size.zero?
  else
    super
  end
end

#handle_dependencyObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_record/associations/has_many_association.rb', line 11

def handle_dependency
  case options[:dependent]
  when :restrict_with_exception
    raise ActiveRecord::DeleteRestrictionError.new(reflection.name) unless empty?

  when :restrict_with_error
    unless empty?
      record = owner.class.human_attribute_name(reflection.name).downcase
      message = owner.errors.generate_message(:base, :'restrict_dependent_destroy.many', record: record, raise: true) rescue nil
      if message
        ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
          The error key `:'restrict_dependent_destroy.many'` has been deprecated and will be removed in Rails 5.1.
          Please use `:'restrict_dependent_destroy.has_many'` instead.
        MESSAGE
      end
      owner.errors.add(:base, message || :'restrict_dependent_destroy.has_many', record: record)
      throw(:abort)
    end

  else
    if options[:dependent] == :destroy
      # No point in executing the counter update since we're going to destroy the parent anyway
      load_target.each { |t| t.destroyed_by_association = reflection }
      destroy_all
    else
      delete_all
    end
  end
end

#insert_record(record, validate = true, raise = false) ⇒ Object



41
42
43
44
# File 'lib/active_record/associations/has_many_association.rb', line 41

def insert_record(record, validate = true, raise = false)
  set_owner_attributes(record)
  super
end