Class: ActiveNode::Associations::CollectionAssociation
- Inherits:
-
Association
- Object
- Association
- ActiveNode::Associations::CollectionAssociation
- Defined in:
- lib/active_node/associations/collection_association.rb
Overview
Active Record Association Collection
CollectionAssociation is an abstract class that provides common stuff to ease the implementation of association proxies that represent collections. See the class hierarchy in AssociationProxy.
CollectionAssociation:
HasAndBelongsToManyAssociation => has_and_belongs_to_many
HasManyAssociation => has_many
HasManyThroughAssociation + ThroughAssociation => has_many :through
CollectionAssociation class provides common methods to the collections defined by has_and_belongs_to_many
, has_many
or has_many
with :through association option.
You need to be careful with assumptions regarding the target: The proxy does not fetch records from the database until it needs them, but new ones created with build
are added to the target. So, the target may be non-empty and still lack children waiting to be read from the database. If you look directly to the database you cannot assume that’s the entire collection because new records may have been added to the target, etc.
If you need to work on all current children, new and existing records, load_target
and the loaded
flag are your friends.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Association
Instance Method Summary collapse
-
#ids_reader ⇒ Object
Implements the ids reader method, e.g.
-
#ids_writer(ids) ⇒ Object
Implements the ids writer method, e.g.
- #reset ⇒ Object
- #target_each ⇒ Object
Methods inherited from Association
#initialize, #klass, #load_target, #reader, #save, #writer
Constructor Details
This class inherits a constructor from ActiveNode::Associations::Association
Instance Method Details
#ids_reader ⇒ Object
Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
30 31 32 |
# File 'lib/active_node/associations/collection_association.rb', line 30 def ids_reader reader.map(&:id) end |
#ids_writer(ids) ⇒ Object
Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items
35 36 37 |
# File 'lib/active_node/associations/collection_association.rb', line 35 def ids_writer(ids) writer klass.find(ids.reject(&:blank?).map!(&:to_i)) end |
#reset ⇒ Object
43 44 45 46 47 |
# File 'lib/active_node/associations/collection_association.rb', line 43 def reset super @target = owner.new_record? ? [] : nil @dirty = false end |
#target_each ⇒ Object
39 40 41 |
# File 'lib/active_node/associations/collection_association.rb', line 39 def target_each target.each {|n| yield n} end |