Class: CouchFoo::Associations::HasManyAssociation

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

Overview

:nodoc:

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

#===, #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 CouchFoo::Associations::AssociationCollection

Dynamic Method Handling

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

Instance Method Details

#count(*args) ⇒ Object

Count the number of associated records. With CouchDB it does not make sense to have a second view for this count as it is likely that at some point the developer will access the objects themselves via find and thus create a suitable view. With CouchDB > 0.8 we can use the reduce function but for earlier versions we just do a find and count the results



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/couch_foo/associations/has_many_association.rb', line 9

def count(*args)
  options = args.extract_options!
  options[:conditions] = @association_conditions.merge(options[:conditions] || {})
  if database.version > 0.8
    value = count_view(options)
  else
    value = find(:all, options).size
  end

  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