Class: ActiveRecord::Associations::HasManyThroughAssociation

Inherits:
HasManyAssociation show all
Includes:
ThroughAssociation
Defined in:
activerecord/lib/active_record/associations/has_many_through_association.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from Association

#inversed, #owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from HasManyAssociation

#empty?, #handle_dependency

Methods included from ForeignAssociation

#foreign_key_present?

Methods inherited from CollectionAssociation

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

Methods inherited from Association

#aliased_table_name, #association_scope, #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

#initialize(owner, reflection) ⇒ HasManyThroughAssociation

Returns a new instance of HasManyThroughAssociation.


9
10
11
12
13
14
# File 'activerecord/lib/active_record/associations/has_many_through_association.rb', line 9

def initialize(owner, reflection)
  super

  @through_records     = {}
  @through_association = nil
end

Instance Method Details

#concat(*records) ⇒ Object


31
32
33
34
35
36
37
38
39
# File 'activerecord/lib/active_record/associations/has_many_through_association.rb', line 31

def concat(*records)
  unless owner.new_record?
    records.flatten.each do |record|
      raise_on_type_mismatch!(record)
    end
  end

  super
end

#concat_records(records) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'activerecord/lib/active_record/associations/has_many_through_association.rb', line 41

def concat_records(records)
  ensure_not_nested

  records = super(records, true)

  if owner.new_record? && records
    records.flatten.each do |record|
      build_through_record(record)
    end
  end

  records
end

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


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'activerecord/lib/active_record/associations/has_many_through_association.rb', line 55

def insert_record(record, validate = true, raise = false)
  ensure_not_nested

  if record.new_record?
    if raise
      record.save!(:validate => validate)
    else
      return unless record.save(:validate => validate)
    end
  end

  save_through_record(record)
  if has_cached_counter? && !through_reflection_updates_counter_cache?
    ActiveSupport::Deprecation.warn(<<-MSG.squish)
      Automatic updating of counter caches on through associations has been
      deprecated, and will be removed in Rails 5. Instead, please set the
      appropriate `counter_cache` options on the `has_many` and `belongs_to`
      for your associations to #{through_reflection.name}.
    MSG

    update_counter_in_database(1)
  end
  record
end

#sizeObject

Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded, and by calling collection.size if it has. If the collection will likely have a size greater than zero, and if fetching the collection will be needed afterwards, one less SELECT query will be generated by using #length instead.


21
22
23
24
25
26
27
28
29
# File 'activerecord/lib/active_record/associations/has_many_through_association.rb', line 21

def size
  if has_cached_counter?
    owner._read_attribute cached_counter_attribute_name(reflection)
  elsif loaded?
    target.size
  else
    super
  end
end