Class: ActiveRecord::Associations::HasManyAssociation
- Inherits:
-
AssociationCollection
- Object
- AssociationProxy
- AssociationCollection
- ActiveRecord::Associations::HasManyAssociation
- Defined in:
- lib/active_record/associations/has_many_association.rb
Overview
:nodoc:
Direct Known Subclasses
Instance Method Summary collapse
-
#count(*args) ⇒ Object
Count the number of associated records.
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.[:counter_sql] @reflection.klass.count_by_sql(@counter_sql) elsif @reflection.[:finder_sql] @reflection.klass.count_by_sql(@finder_sql) else column_name, = @reflection.klass.send(:construct_count_options_from_args, *args) [:conditions] = [:conditions].blank? ? @finder_sql : @finder_sql + " AND (#{sanitize_sql([:conditions])})" [:include] ||= @reflection.[:include] value = @reflection.klass.count(column_name, ) limit = @reflection.[:limit] offset = @reflection.[:offset] if limit || offset [ [value - offset.to_i, 0].max, limit.to_i ].min else value end end end |