Class: ActiveFedora::Associations::HasManyAssociation
- Inherits:
-
CollectionAssociation
- Object
- Association
- CollectionAssociation
- ActiveFedora::Associations::HasManyAssociation
- Defined in:
- lib/active_fedora/associations/has_many_association.rb
Overview
:nodoc:
Instance Attribute Summary
Attributes inherited from CollectionAssociation
Attributes inherited from Association
Instance Method Summary collapse
-
#count_records ⇒ Object
Returns the number of records in this collection.
-
#initialize(owner, reflection) ⇒ HasManyAssociation
constructor
A new instance of HasManyAssociation.
- #insert_record(record, force = false, validate = true) ⇒ Object
Methods inherited from CollectionAssociation
#<<, #add_record_to_target_with_callbacks, #add_to_target, #any?, #build, #count, #create, #create!, #delete, #delete_all, #destroy, #destroy_all, #empty?, #find, #find_target, #first, #ids_reader, #ids_writer, #include?, #last, #load_from_solr, #load_target, #merge_target_lists, #null_scope?, #reader, #replace, #reset, #scope, #size, #to_ary, #writer
Methods inherited from Association
#association_scope, #load_target, #loaded!, #loaded?, #reload, #reset, #scope, #set_inverse_instance, #stale_target?, #target_scope
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 |
Instance Method Details
#count_records ⇒ Object
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 |
# 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) 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
31 32 33 34 |
# File 'lib/active_fedora/associations/has_many_association.rb', line 31 def insert_record(record, force = false, validate = true) set_belongs_to_association_for(record) record.save end |