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 CollectionAssociation

#proxy

Attributes inherited from Association

#owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from CollectionAssociation

#add_to_target, #any?, #build, #count, #create, #create!, #delete, #delete_all, #destroy, #destroy_all, #empty?, #find, #first, #ids_reader, #ids_writer, #include?, #initialize, #last, #length, #load_target, #many?, #reader, #replace, #reset, #select, #sum, #transaction, #uniq, #writer

Methods inherited from Association

#aliased_table_name, #association_scope, #initialize, #interpolate, #klass, #load_target, #loaded!, #loaded?, #reload, #reset, #reset_scope, #scoped, #set_inverse_instance, #stale_target?, #target_scope

Constructor Details

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

Instance Method Details

#concat(*records) ⇒ Object



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

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

  super
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'activerecord/lib/active_record/associations/has_many_through_association.rb', line 34

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

  through_record(record).save!
  update_counter(1)
  record
end

#sizeObject

Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn’t been loaded and calling collection.size if it has. If it’s more likely than not that the collection does have a size larger than zero, and you need to fetch that collection afterwards, it’ll take one fewer SELECT query if you use #length.



13
14
15
16
17
18
19
20
21
# File 'activerecord/lib/active_record/associations/has_many_through_association.rb', line 13

def size
  if has_cached_counter?
    owner.send(:read_attribute, cached_counter_attribute_name)
  elsif loaded?
    target.size
  else
    count
  end
end