Class: ActiveRecord::Associations::HasManyAssociation

Inherits:
AssociationCollection show all
Defined in:
lib/active_record/associations/has_many_association.rb

Overview

:nodoc:

Direct Known Subclasses

HasManyThroughAssociation

Instance Method Summary collapse

Methods inherited from AssociationCollection

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

Methods inherited from AssociationProxy

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

Constructor Details

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

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

Count the number of associated records. All arguments are optional.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_record/associations/has_many_association.rb', line 5

def count(*args)
  if @reflection.options[:counter_sql]
    @reflection.klass.count_by_sql(@counter_sql)
  elsif @reflection.options[:finder_sql]
    @reflection.klass.count_by_sql(@finder_sql)
  else
    column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args)          
    options[:conditions] = options[:conditions].blank? ?
      @finder_sql :
      @finder_sql + " AND (#{sanitize_sql(options[:conditions])})"
    options[:include] ||= @reflection.options[:include]

    value = @reflection.klass.count(column_name, options)

    limit  = @reflection.options[:limit]
    offset = @reflection.options[:offset]

    if limit || offset
      [ [value - offset.to_i, 0].max, limit.to_i ].min
    else
      value
    end
  end
end