Class: ActiveRecord::Associations::HasManyThroughAssociation

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

Overview

:nodoc:

Direct Known Subclasses

HasOneThroughAssociation

Instance Method Summary collapse

Methods inherited from AssociationCollection

#<<, #any?, #build, #clear, #delete, #delete_all, #destroy_all, #empty?, #find, #first, #include?, #last, #length, #replace, #reset, #sum, #to_ary, #uniq

Methods inherited from AssociationProxy

#===, #aliased_table_name, #inspect, #loaded, #loaded?, #proxy_owner, #proxy_reflection, #proxy_respond_to?, #proxy_target, #reload, #reset, #respond_to?, #target, #target=

Constructor Details

#initialize(owner, reflection) ⇒ HasManyThroughAssociation

Returns a new instance of HasManyThroughAssociation.



4
5
6
7
# File 'lib/active_record/associations/has_many_through_association.rb', line 4

def initialize(owner, reflection)
  reflection.check_validity!
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveRecord::Associations::AssociationCollection

Instance Method Details

#count(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/active_record/associations/has_many_through_association.rb', line 34

def count(*args)
  column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args)
  if @reflection.options[:uniq]
    # This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL statement.
    column_name = "#{@reflection.quoted_table_name}.#{@reflection.klass.primary_key}" if column_name == :all
    options.merge!(:distinct => true) 
  end
  @reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) } 
end

#create(attrs = nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/active_record/associations/has_many_through_association.rb', line 18

def create(attrs = nil)
  @reflection.klass.transaction do
    self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.klass.create } : @reflection.klass.create)
    object
  end
end

#create!(attrs = nil) ⇒ Object



11
12
13
14
15
16
# File 'lib/active_record/associations/has_many_through_association.rb', line 11

def create!(attrs = nil)
  @reflection.klass.transaction do
    self << (object = attrs ? @reflection.klass.send(:with_scope, :create => attrs) { @reflection.klass.create! } : @reflection.klass.create!)
    object
  end
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 less SELECT query if you use length.



28
29
30
31
32
# File 'lib/active_record/associations/has_many_through_association.rb', line 28

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