Class: ActiveFedora::Associations::HasManyAssociation

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

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from AssociationCollection

#<<, #add_record_to_target_with_callbacks, #build, #delete, #find_target, #load_from_solr, #load_target, #replace, #reset, #size, #to_ary

Methods inherited from AssociationProxy

#loaded, #loaded?, #reload, #reset, #target, #target=

Constructor Details

#initialize(owner, reflection) ⇒ HasManyAssociation

Returns a new instance of HasManyAssociation.



4
5
6
# File 'lib/active_fedora/associations/has_many_association.rb', line 4

def initialize(owner, reflection)
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveFedora::Associations::AssociationProxy

Instance Method Details

#count_recordsObject

Returns the number of records in this collection.

That does not depend on whether the collection has already been loaded or not. The size method is the one that takes the loaded flag into account and delegates to count_records if needed.

If the collection is empty the target is set to an empty array and the loaded flag is set to true as well.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_fedora/associations/has_many_association.rb', line 16

def count_records
  count = if loaded? 
    @target.size
  else
    @reflection.klass.count(:conditions => @counter_query)
    # load_target
    # @target.size
  end

  # If there's nothing in the database and @target has no new records
  # we are certain the current target is an empty array. This is a
  # documented side-effect of the method that may avoid an extra SELECT.
  @target ||= [] and loaded if count == 0

  return count
end

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



33
34
35
36
37
# File 'lib/active_fedora/associations/has_many_association.rb', line 33

def insert_record(record, force = false, validate = true)
  set_belongs_to_association_for(record)
  #force ? record.save! : record.save(:validate => validate)
  record.save
end